Skip to content

Commit 5372efa

Browse files
committed
Merge remote-tracking branch 'upstream/master' into JsonResult
# Conflicts: # src/FluentAssertions.AspNetCore.Mvc/FailureMessages.Designer.cs # src/FluentAssertions.AspNetCore.Mvc/FailureMessages.resx # src/FluentAssertions.AspNetCore.Mvc/FluentAssertions.AspNetCore.Mvc.csproj
2 parents b7c7ec2 + 207be66 commit 5372efa

File tree

6 files changed

+157
-0
lines changed

6 files changed

+157
-0
lines changed

Diff for: src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs

+28
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,34 @@ public RedirectToActionResultAssertions BeRedirectToActionResult(string reason,
256256
return new RedirectToActionResultAssertions(Subject as RedirectToActionResult);
257257
}
258258

259+
/// <summary>
260+
/// Asserts that the subject is a <see cref="StatusCodeResult"/>.
261+
/// </summary>
262+
public StatusCodeResultAssertions BeStatusCodeResult()
263+
{
264+
return BeStatusCodeResult(string.Empty, null);
265+
}
266+
267+
/// <summary>
268+
/// Asserts that the subject is a <see cref="StatusCodeResult"/>.
269+
/// </summary>
270+
/// <param name="reason">
271+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
272+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
273+
/// </param>
274+
/// <param name="reasonArgs">
275+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
276+
/// </param>
277+
public StatusCodeResultAssertions BeStatusCodeResult(string reason, params object[] reasonArgs)
278+
{
279+
Execute.Assertion
280+
.BecauseOf(reason, reasonArgs)
281+
.ForCondition(Subject is StatusCodeResult)
282+
.FailWith(Constants.CommonFailMessage, "StatusCodeResult", Subject.GetType().Name);
283+
284+
return new StatusCodeResultAssertions(Subject as StatusCodeResult);
285+
}
286+
259287
#endregion Public Methods
260288

261289
#region Public Structs

Diff for: src/FluentAssertions.AspNetCore.Mvc/FailureMessages.Designer.cs

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/FluentAssertions.AspNetCore.Mvc/FailureMessages.resx

+6
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@
144144
<data name="RouteData_Values_HaveValue" xml:space="preserve">
145145
<value>Expected RouteData.Values[{0}] to have value {1}, but found {2}.</value>
146146
</data>
147+
<data name="StatusCodeResultBase_WithStatusCode" xml:space="preserve">
148+
<value>Expected status code to be {0} but found {1}.</value>
149+
</data>
150+
<data name="ViewResultBase_NullModel" xml:space="preserve">
151+
<value>Expected Model to be of type {0}, but no Model was supplied.</value>
152+
</data>
147153
<data name="ViewResultBase_ViewData_ContainsKey" xml:space="preserve">
148154
<value>ViewData does not contain key of {0}.</value>
149155
</data>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using FluentAssertions.Execution;
2+
using FluentAssertions.Primitives;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace FluentAssertions.AspNetCore.Mvc
6+
{
7+
public class StatusCodeResultAssertions : ObjectAssertions
8+
{
9+
/// <summary>
10+
/// Initializes a new instance of the <see cref="T:StatusCodeResultAssertions" /> class.
11+
/// </summary>
12+
/// <param name="subject">The object to test assertion on</param>
13+
public StatusCodeResultAssertions(StatusCodeResult subject) : base(subject)
14+
{
15+
}
16+
17+
private StatusCodeResult StatusCodeResultSubject => (StatusCodeResult) Subject;
18+
19+
/// <summary>
20+
/// The model.
21+
/// </summary>
22+
public int StatusCode => StatusCodeResultSubject.StatusCode;
23+
24+
/// <summary>
25+
/// Asserts that the status code is the expected status code.
26+
/// </summary>
27+
/// <param name="expectedStatusCode">The status code.</param>
28+
/// <param name="reason">
29+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
30+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
31+
/// </param>
32+
/// <param name="reasonArgs">
33+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
34+
/// </param>
35+
public StatusCodeResultAssertions WithStatusCode(int expectedStatusCode, string reason = "", params object[] reasonArgs)
36+
{
37+
Execute.Assertion
38+
.ForCondition(StatusCode == expectedStatusCode)
39+
.BecauseOf(reason, reasonArgs)
40+
.FailWith(FailureMessages.StatusCodeResultBase_WithStatusCode, expectedStatusCode, StatusCode);
41+
return this;
42+
}
43+
}
44+
}

Diff for: tests/FluentAssertions.AspNetCore.Mvc.Tests/ActionResultAssertions_Tests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ public void BeView_GivenNotView_ShouldFail()
123123
.WithMessage("Expected ActionResult to be \"ViewResult\", but found \"RedirectResult\"");
124124
}
125125

126+
[Fact]
127+
public void BeStatusCodeResult_GivenStatusCodeResult_ShouldPass()
128+
{
129+
ActionResult result = new StatusCodeResult(200);
130+
result.Should().BeStatusCodeResult();
131+
}
132+
133+
[Fact]
134+
public void BeStatusCodeResult_GivenNotStatusCodeResult_ShouldFail()
135+
{
136+
ActionResult result = new RedirectResult("/");
137+
Action a = () => result.Should().BeStatusCodeResult();
138+
a.Should().Throw<Exception>()
139+
.WithMessage("Expected ActionResult to be \"StatusCodeResult\", but found \"RedirectResult\"");
140+
}
141+
126142
#endregion Public Methods
127143
}
128144
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using FluentAssertions.Mvc.Tests.Fakes;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System;
4+
using Xunit;
5+
6+
namespace FluentAssertions.AspNetCore.Mvc.Tests
7+
{
8+
public class StatusCodeResultAssertions_Tests
9+
{
10+
#region Private Fields
11+
12+
private FakeController _controller = new FakeController();
13+
14+
#endregion Private Fields
15+
16+
#region Public Methods
17+
18+
[Fact]
19+
public void WithStatusCode_GivenExpectedValue_ShouldPass()
20+
{
21+
int statusCode = 200;
22+
23+
ActionResult result = new StatusCodeResult(statusCode);
24+
25+
result.Should().BeStatusCodeResult().WithStatusCode(statusCode);
26+
}
27+
28+
[Fact]
29+
public void WithStatusCode_GivenUnexpectedValue_ShouldFail()
30+
{
31+
int actualStatusCode = 200;
32+
int expectedStatusCode = 400;
33+
34+
var failureMessage = string.Format(FailureMessages.StatusCodeResultBase_WithStatusCode, expectedStatusCode, actualStatusCode);
35+
ActionResult result = new StatusCodeResult(actualStatusCode);
36+
37+
Action action = () => result.Should().BeStatusCodeResult().WithStatusCode(expectedStatusCode);
38+
39+
action.Should().Throw<Exception>()
40+
.WithMessage(failureMessage);
41+
}
42+
43+
#endregion Public Methods
44+
}
45+
}

0 commit comments

Comments
 (0)