Skip to content

Commit c523d4b

Browse files
authored
Merge pull request #11 from faddiv/JsonResult
Json result
2 parents 207be66 + 5372efa commit c523d4b

12 files changed

+349
-27
lines changed

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

+28
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,34 @@ public EmptyResult BeEmptyResult(string reason, params object[] reasonArgs)
8686
return Subject as EmptyResult;
8787
}
8888

89+
/// <summary>
90+
/// Asserts that the subject is an <see cref="JsonResult"/>.
91+
/// </summary>
92+
public JsonResultAssertions BeJsonResult()
93+
{
94+
return BeJsonResult(string.Empty, null);
95+
}
96+
97+
/// <summary>
98+
/// Asserts that the subject is an <see cref="JsonResult"/>.
99+
/// </summary>
100+
/// <param name="reason">
101+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
102+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
103+
/// </param>
104+
/// <param name="reasonArgs">
105+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
106+
/// </param>
107+
public JsonResultAssertions BeJsonResult(string reason, params object[] reasonArgs)
108+
{
109+
Execute.Assertion
110+
.BecauseOf(reason, reasonArgs)
111+
.ForCondition(Subject is JsonResult)
112+
.FailWith(Constants.CommonFailMessage, typeof(JsonResult).Name, Subject.GetType().Name);
113+
114+
return new JsonResultAssertions(Subject as JsonResult);
115+
}
116+
89117
/// <summary>
90118
/// Asserts that the subject is a <see cref="RedirectToRouteResult"/>.
91119
/// </summary>

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

+35-17
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
@@ -120,6 +120,12 @@
120120
<data name="CommonFailMessage" xml:space="preserve">
121121
<value>Expected {0} to be '{1}' but found '{2}'</value>
122122
</data>
123+
<data name="CommonNullWasSuppliedFailMessage" xml:space="preserve">
124+
<value>Expected {0} to be of type {1}, but no {0} was supplied.</value>
125+
</data>
126+
<data name="CommonTypeFailMessage" xml:space="preserve">
127+
<value>Expected {0} to be of type '{1}' but was '{2}'</value>
128+
</data>
123129
<data name="RedirectToActionResult_RouteValues_ContainsKey" xml:space="preserve">
124130
<value>RedirectToActionResult.RouteValues does not contain key {0}.</value>
125131
</data>

Diff for: src/FluentAssertions.AspNetCore.Mvc/FluentAssertions.AspNetCore.Mvc.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Fluent Assertions extensions for ASP.NET Core MVC.</Description>
@@ -39,7 +39,7 @@
3939

4040
<ItemGroup>
4141
<EmbeddedResource Update="FailureMessages.resx">
42-
<Generator>PublicResXFileCodeGenerator</Generator>
42+
<Generator>ResXFileCodeGenerator</Generator>
4343
<LastGenOutput>FailureMessages.Designer.cs</LastGenOutput>
4444
</EmbeddedResource>
4545
</ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using FluentAssertions.Execution;
2+
using FluentAssertions.Primitives;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Newtonsoft.Json;
5+
using System;
6+
using System.Diagnostics;
7+
8+
namespace FluentAssertions.AspNetCore.Mvc
9+
{
10+
/// <summary>
11+
/// Contains a number of methods to assert that a <see cref="JsonResult" /> is in the expected state.
12+
/// </summary>
13+
[DebuggerNonUserCode]
14+
public class JsonResultAssertions : ObjectAssertions
15+
{
16+
#region Public Constructors
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="T:JsonResultAssertions" /> class.
20+
/// </summary>
21+
/// <param name="subject">The object to test assertion on</param>
22+
public JsonResultAssertions(JsonResult subject) : base(subject)
23+
{
24+
25+
}
26+
27+
#endregion
28+
29+
#region Public Properties
30+
31+
/// <summary>
32+
/// The serializer settings of the JsonResult.
33+
/// </summary>
34+
public JsonSerializerSettings SerializerSettings => JsonResultSubject.SerializerSettings;
35+
36+
/// <summary>
37+
/// The value on the JsonResult
38+
/// </summary>
39+
public object Value => JsonResultSubject.Value;
40+
41+
#endregion
42+
43+
#region Private Properties
44+
45+
private JsonResult JsonResultSubject => (JsonResult)Subject;
46+
47+
#endregion Private Properties
48+
49+
#region Public Methods
50+
51+
/// <summary>
52+
/// Asserts that the content type is the expected content type.
53+
/// </summary>
54+
/// <param name="expectedContentType">The expected content type.</param>
55+
/// <param name="reason">
56+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
57+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
58+
/// </param>
59+
/// <param name="reasonArgs">
60+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
61+
/// </param>
62+
public JsonResultAssertions WithContentType(string expectedContentType, string reason = "",
63+
params object[] reasonArgs)
64+
{
65+
var actualContentType = JsonResultSubject.ContentType;
66+
67+
Execute.Assertion
68+
.ForCondition(string.Equals(expectedContentType, actualContentType, StringComparison.OrdinalIgnoreCase))
69+
.BecauseOf(reason, reasonArgs)
70+
.FailWith(FailureMessages.CommonFailMessage, "JsonResult.ContentType", expectedContentType, actualContentType);
71+
return this;
72+
}
73+
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+
97+
/// <summary>
98+
/// Asserts the value is of the expected type.
99+
/// </summary>
100+
/// <typeparam name="TValue">The expected type.</typeparam>
101+
/// <returns>The typed value.</returns>
102+
public TValue ValueAs<TValue>()
103+
{
104+
var value = JsonResultSubject.Value;
105+
106+
if (value == null)
107+
Execute.Assertion.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, "Value", typeof(TValue).Name);
108+
109+
Execute.Assertion
110+
.ForCondition(value is TValue)
111+
.FailWith(FailureMessages.CommonTypeFailMessage, "Value", typeof(TValue).Name, value.GetType().Name);
112+
113+
return (TValue)value;
114+
}
115+
#endregion
116+
}
117+
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public PartialViewResultAssertions(PartialViewResult subject) : base(subject)
1515
{
1616
}
1717

18-
private PartialViewResult PartialViewResultSubject => (PartialViewResult) Subject;
18+
private PartialViewResult PartialViewResultSubject => (PartialViewResult)Subject;
1919

2020
/// <summary>
2121
/// The model.
@@ -114,13 +114,13 @@ public TModel ModelAs<TModel>()
114114
var model = PartialViewResultSubject.ViewData?.Model;
115115

116116
if (model == null)
117-
Execute.Assertion.FailWith(FailureMessages.ViewResultBase_NullModel, typeof(TModel).Name);
117+
Execute.Assertion.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, "Model", typeof(TModel).Name);
118118

119119
Execute.Assertion
120120
.ForCondition(model is TModel)
121-
.FailWith("Expected Model to be of type '{0}' but was '{1}'", typeof(TModel).Name, model.GetType().Name);
121+
.FailWith(FailureMessages.CommonTypeFailMessage, "Model", typeof(TModel).Name, model.GetType().Name);
122122

123-
return (TModel) model;
123+
return (TModel)model;
124124
}
125125

126126
/// <summary>

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ public TModel ModelAs<TModel>()
133133
var model = ViewResultSubject.Model;
134134

135135
if (model == null)
136-
Execute.Assertion.FailWith(FailureMessages.ViewResultBase_NullModel, typeof(TModel).Name);
136+
Execute.Assertion.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, "Model", typeof(TModel).Name);
137137

138138
Execute.Assertion
139139
.ForCondition(model is TModel)
140-
.FailWith("Expected Model to be of type '{0}' but was '{1}'", typeof(TModel).Name, model.GetType().Name);
140+
.FailWith(FailureMessages.CommonTypeFailMessage, "Model", typeof(TModel).Name, model.GetType().Name);
141141

142142
return (TModel)model;
143143
}

0 commit comments

Comments
 (0)