Skip to content

Commit b7c7ec2

Browse files
committed
Add WithStatusCode to JsonResultAssertions.
1 parent 56cfb3c commit b7c7ec2

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/FluentAssertions.AspNetCore.Mvc/JsonResultAssertions.cs

+23
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@ public JsonResultAssertions WithContentType(string expectedContentType, string r
7171
return this;
7272
}
7373

74+
/// <summary>
75+
/// Asserts that the status code is the expected status code.
76+
/// </summary>
77+
/// <param name="statusCode">The expected status code.</param>
78+
/// <param name="reason">
79+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
80+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
81+
/// </param>
82+
/// <param name="reasonArgs">
83+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
84+
/// </param>
85+
public JsonResultAssertions WithStatusCode(int? expectedStatusCode, string reason = "",
86+
params object[] reasonArgs)
87+
{
88+
var actualStatusCode = JsonResultSubject.StatusCode;
89+
90+
Execute.Assertion
91+
.ForCondition(expectedStatusCode == actualStatusCode)
92+
.BecauseOf(reason, reasonArgs)
93+
.FailWith(FailureMessages.CommonFailMessage, "JsonResult.StatusCode", expectedStatusCode, actualStatusCode);
94+
return this;
95+
}
96+
7497
/// <summary>
7598
/// Asserts the value is of the expected type.
7699
/// </summary>

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

+29-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ public void WithContentType_GivenUnexpected_ShouldFail()
3636
.WithMessage(failureMessage);
3737
}
3838

39+
[Fact]
40+
public void WithStatusCode_GivenValue_ShouldPass()
41+
{
42+
ActionResult result = new JsonResult("value")
43+
{
44+
StatusCode = 200
45+
};
46+
47+
result.Should().BeJsonResult().WithStatusCode(200);
48+
}
49+
50+
[Fact]
51+
public void WithStatusCode_GivenUnexpected_ShouldFail()
52+
{
53+
var actualStatusCode = 401;
54+
var expectedStatusCode = 200;
55+
ActionResult result = new JsonResult("value")
56+
{
57+
StatusCode = actualStatusCode
58+
};
59+
var failureMessage = string.Format(FailureMessages.CommonFailMessage, "\"JsonResult.StatusCode\"", expectedStatusCode, actualStatusCode);
60+
61+
Action a = () => result.Should().BeJsonResult().WithStatusCode(expectedStatusCode);
62+
63+
a.Should().Throw<Exception>()
64+
.WithMessage(failureMessage);
65+
}
66+
3967
[Fact]
4068
public void Value_GivenExpectedValue_ShouldPass()
4169
{
@@ -45,7 +73,7 @@ public void Value_GivenExpectedValue_ShouldPass()
4573
}
4674

4775
[Fact]
48-
public void Json_GivenUnexpectedValue_ShouldFail()
76+
public void Value_GivenUnexpectedValue_ShouldFail()
4977
{
5078
var result = new TestController().JsonSimpleValue();
5179

0 commit comments

Comments
 (0)