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
+ }
0 commit comments