Skip to content

Commit 3d09bb1

Browse files
committed
Add ObjectAssertions.WithStatusCode. Simplify FailureMessageHelper.
1 parent b6abf8d commit 3d09bb1

File tree

4 files changed

+64
-46
lines changed

4 files changed

+64
-46
lines changed

src/FluentAssertions.AspNetCore.Mvc/ObjectResultAssertionsBase.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,25 +142,50 @@ public TObjectResultAssertion WithContentType(string expected, string reason = "
142142
}
143143

144144
/// <summary>
145-
/// Asserts that the <see cref="ObjectResult.DeclaredType"/> contains the specified content type.
145+
/// Asserts that the <see cref="ObjectResult.DeclaredType"/> is the expected declared type.
146146
/// </summary>
147-
/// <param name="expected">The expectation content type.</param>
147+
/// <param name="expectedDeclaredType">The expected declared type.</param>
148148
/// <param name="reason">
149149
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
150150
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
151151
/// </param>
152152
/// <param name="reasonArgs">
153153
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
154154
/// </param>
155-
public TObjectResultAssertion WithDeclaredType(Type expected, string reason = "", params object[] reasonArgs)
155+
public TObjectResultAssertion WithDeclaredType(Type expectedDeclaredType, string reason = "", params object[] reasonArgs)
156156
{
157157
var actual = ObjectResultSubject.DeclaredType;
158158

159159
Execute.Assertion
160-
.ForCondition(expected == actual)
160+
.ForCondition(expectedDeclaredType == actual)
161161
.WithDefaultIdentifier(Identifier + ".DeclaredType")
162162
.BecauseOf(reason, reasonArgs)
163-
.FailWith(FailureMessages.CommonTypeFailMessage, expected, actual);
163+
.FailWith(FailureMessages.CommonTypeFailMessage, expectedDeclaredType, actual);
164+
165+
return (TObjectResultAssertion)this;
166+
}
167+
168+
169+
/// <summary>
170+
/// Asserts that the <see cref="ObjectResult.StatusCode"/> is the expected status code.
171+
/// </summary>
172+
/// <param name="expectedStatusCode">The expected status code.</param>
173+
/// <param name="reason">
174+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
175+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
176+
/// </param>
177+
/// <param name="reasonArgs">
178+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
179+
/// </param>
180+
public TObjectResultAssertion WithStatusCode(int? expectedStatusCode, string reason = "", params object[] reasonArgs)
181+
{
182+
var actual = ObjectResultSubject.StatusCode;
183+
184+
Execute.Assertion
185+
.ForCondition(expectedStatusCode == actual)
186+
.WithDefaultIdentifier(Identifier + ".StatusCode")
187+
.BecauseOf(reason, reasonArgs)
188+
.FailWith(FailureMessages.CommonFailMessage, expectedStatusCode, actual);
164189

165190
return (TObjectResultAssertion)this;
166191
}

tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedResultAssertions_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void WithUri_GivenExpectedUri_ShouldPass()
7777
public void WithUri_GivenWrongUri_ShouldFail()
7878
{
7979
var result = new TestController().Created(TestWrongUri, TestValue);
80-
var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("CreatedResult.Uri", TestUri, TestWrongUri);
80+
var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("CreatedResult.Uri", TestUri.ToString(), TestWrongUri.ToString());
8181

8282
Action a = () => result.Should().BeCreatedResult().WithUri(TestUri, Reason, ReasonArgs);
8383

tests/FluentAssertions.AspNetCore.Mvc.Tests/Helpers/FailureMessageHelper.cs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,9 @@ public static string Format(string message, params string[] args)
1818
return String.Format(message, formattedArg);
1919
}
2020

21-
public static string ExpectedContextToBeXButY(string context, Uri expected, Uri actual)
22-
{
23-
return ExpectedContextToBeXButY(context, expected?.ToString(), actual?.ToString());
24-
}
25-
26-
public static string ExpectedContextToBeXButY(string context, string expected, string actual)
27-
{
28-
return ExpectedContextToBeXButY(context, (object)$"\"{expected}\"", (object)$"\"{actual}\"");
29-
}
30-
31-
public static string ExpectedContextToBeXButY(string context, DateTimeOffset? expected, DateTimeOffset? actual)
32-
{
33-
return ExpectedContextToBeXButY(context, ToString(expected), ToString(actual));
34-
}
35-
3621
public static string ExpectedContextToBeXButY(string context, object expected, object actual)
3722
{
38-
return $"Expected {context} to be {expected} because it is 10 but found {actual}.";
23+
return $"Expected {context} to be {Formatter.ToString(expected)} because it is 10 but found {Formatter.ToString(actual)}.";
3924
}
4025

4126
public static string ExpectedAtKeyValueXButFoundY(string context, string key, string expected, string actual)
@@ -48,29 +33,6 @@ public static string ExpectedKeyButNotFound(string context, string key, string e
4833
return $"Expected {context} to contain value \"{expected}\" at key \"{key}\" because it is 10, but the key was not found.";
4934
}
5035

51-
private static object ToString(DateTimeOffset? expected)
52-
{
53-
if (expected.HasValue)
54-
{
55-
var builder = new StringBuilder($"<{expected:yyyy-MM-dd HH:mm:ss}");
56-
if (expected.Value.Millisecond > 0)
57-
{
58-
builder.AppendFormat(".{0:fffffff}", expected.Value);
59-
}
60-
if (expected.Value.Offset.Hours > 0)
61-
{
62-
builder.AppendFormat(" +{0}h", expected.Value.Offset.Hours);
63-
}
64-
else if (expected.Value.Offset.Hours < 0)
65-
{
66-
builder.AppendFormat(" -{0}h", expected.Value.Offset.Hours);
67-
}
68-
builder.Append('>');
69-
return builder.ToString();
70-
}
71-
return "<null>";
72-
}
73-
7436
internal static string ExpectedToContainItemButFoundNull(string context, object item)
7537
{
7638
return $"Expected {context} to contain {Formatter.ToString(item)} because it is 10 but found <null>.";

tests/FluentAssertions.AspNetCore.Mvc.Tests/ObjectResultAssertions_Tests.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ public void WithContentType_GivenUnexpected_ShouldFail()
156156
.WithMessage(failureMessage);
157157
}
158158

159-
160159
[Fact]
161160
public void WithDeclaredType_GivenExpected_ShouldPass()
162161
{
@@ -188,5 +187,37 @@ public void WithDeclaredType_GivenUnexpected_ShouldFail()
188187
a.Should().Throw<Exception>()
189188
.WithMessage(failureMessage);
190189
}
190+
191+
[Fact]
192+
public void WithStatusCode_GivenExpected_ShouldPass()
193+
{
194+
var expectedStatusCode = 200;
195+
var result = new ObjectResult(TestValue)
196+
{
197+
StatusCode = expectedStatusCode
198+
};
199+
200+
result.Should().BeObjectResult().WithStatusCode(expectedStatusCode);
201+
}
202+
203+
[Fact]
204+
public void WithStatusCode_GivenUnexpected_ShouldFail()
205+
{
206+
var expectedStatusCode = 200;
207+
var actualStatusCode = 500;
208+
var result = new ObjectResult(TestValue)
209+
{
210+
StatusCode = actualStatusCode
211+
};
212+
string failureMessage = FailureMessageHelper.ExpectedContextToBeXButY(
213+
"ObjectResult.StatusCode",
214+
expectedStatusCode,
215+
actualStatusCode);
216+
217+
Action a = () => result.Should().BeObjectResult().WithStatusCode(expectedStatusCode, Reason, ReasonArgs);
218+
219+
a.Should().Throw<Exception>()
220+
.WithMessage(failureMessage);
221+
}
191222
}
192223
}

0 commit comments

Comments
 (0)