Skip to content

Commit cf8e22c

Browse files
committed
Add BeJsonResult and WithValue assertions.
1 parent 56286e0 commit cf8e22c

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ public EmptyResult BeEmptyResult(string reason, params object[] reasonArgs)
8686
return Subject as EmptyResult;
8787
}
8888

89+
public JsonResultAssertions BeJsonResult()
90+
{
91+
return BeJsonResult(string.Empty, null);
92+
}
93+
94+
public JsonResultAssertions BeJsonResult(string reason, params object[] reasonArgs)
95+
{
96+
Execute.Assertion
97+
.BecauseOf(reason, reasonArgs)
98+
.ForCondition(Subject is JsonResult)
99+
.FailWith(Constants.CommonFailMessage, typeof(JsonResult).Name, Subject.GetType().Name);
100+
101+
return new JsonResultAssertions(Subject as JsonResult);
102+
}
103+
89104
/// <summary>
90105
/// Asserts that the subject is a <see cref="RedirectToRouteResult"/>.
91106
/// </summary>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using FluentAssertions.Execution;
2+
using FluentAssertions.Primitives;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
6+
namespace FluentAssertions.AspNetCore.Mvc
7+
{
8+
public class JsonResultAssertions : ObjectAssertions
9+
{
10+
#region Public Constructors
11+
12+
public JsonResultAssertions(JsonResult subject) : base(subject)
13+
{
14+
15+
}
16+
17+
#endregion
18+
19+
#region Public Properties
20+
21+
#endregion
22+
23+
#region Private Properties
24+
25+
private JsonResult JsonResultSubject => (JsonResult)Subject;
26+
27+
#endregion Private Properties
28+
29+
#region Public Methods
30+
31+
public JsonResultAssertions WithValue(object expectedValue, string reason = "",
32+
params object[] reasonArgs)
33+
{
34+
var actualValue = JsonResultSubject.Value;
35+
36+
Execute.Assertion
37+
.ForCondition(Equals(expectedValue, actualValue))
38+
.BecauseOf(reason, reasonArgs)
39+
.FailWith(FailureMessages.CommonFailMessage, "JsonResult.Value", expectedValue, actualValue);
40+
return this;
41+
}
42+
43+
#endregion
44+
}
45+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ public void BeEmpty_GivenNotEmpty_ShouldPass()
4242
.WithMessage("Expected ActionResult to be \"EmptyResult\", but found \"ViewResult\"");
4343
}
4444

45+
[Fact]
46+
public void BeJson_GivenJson_ShouldPass()
47+
{
48+
ActionResult result = new JsonResult(new object());
49+
result.Should()
50+
.BeJsonResult();
51+
}
52+
53+
[Fact]
54+
public void BeJson_GivenNotJson_ShouldFail()
55+
{
56+
ActionResult result = new ViewResult();
57+
Action a = () => result.Should().BeJsonResult();
58+
a.Should().Throw<Exception>()
59+
.WithMessage("Expected ActionResult to be \"JsonResult\", but found \"ViewResult\"");
60+
}
61+
4562
[Fact]
4663
public void BeRedirectToRoute_GivenRedirectToRoute_ShouldPass()
4764
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using FluentAssertions.Mvc.Tests.Helpers;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System;
4+
using Xunit;
5+
6+
namespace FluentAssertions.AspNetCore.Mvc.Tests
7+
{
8+
public class JsonResultAssertions_Tests
9+
{
10+
[Fact]
11+
public void WithValue_GivenValue_ShouldPass()
12+
{
13+
ActionResult result = new JsonResult("value");
14+
result.Should().BeJsonResult().WithValue("value");
15+
}
16+
17+
[Fact]
18+
public void WithValue_GivenUnexpected_ShouldFail()
19+
{
20+
var actualValue = "xyz";
21+
var expectedValue = "value";
22+
ActionResult result = new JsonResult(actualValue);
23+
var failureMessage = FailureMessageHelper.Format(FailureMessages.CommonFailMessage, "JsonResult.Value", expectedValue, actualValue);
24+
25+
Action a = () => result.Should().BeJsonResult().WithValue(expectedValue);
26+
27+
a.Should().Throw<Exception>()
28+
.WithMessage(failureMessage);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)