1
- using FluentAssertions . Execution ;
1
+ using FluentAssertions . Common ;
2
+ using FluentAssertions . Equivalency ;
3
+ using FluentAssertions . Execution ;
2
4
using FluentAssertions . Primitives ;
3
5
using Microsoft . AspNetCore . Mvc ;
4
6
using Microsoft . AspNetCore . Mvc . Formatters ;
@@ -157,14 +159,108 @@ public TObjectResultAssertion WithDeclaredType(Type expectedDeclaredType, string
157
159
var actual = ObjectResultSubject . DeclaredType ;
158
160
159
161
Execute . Assertion
162
+ . BecauseOf ( reason , reasonArgs )
160
163
. ForCondition ( expectedDeclaredType == actual )
161
164
. WithDefaultIdentifier ( Identifier + ".DeclaredType" )
162
- . BecauseOf ( reason , reasonArgs )
163
165
. FailWith ( FailureMessages . CommonTypeFailMessage , expectedDeclaredType , actual ) ;
164
166
165
167
return ( TObjectResultAssertion ) this ;
166
168
}
167
169
170
+ /// <summary>
171
+ /// Asserts that the <see cref="ObjectResult.Value"/> is the expected value.
172
+ /// </summary>
173
+ /// <param name="expectedValue">The expected value.</param>
174
+ /// <param name="reason">
175
+ /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
176
+ /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
177
+ /// </param>
178
+ /// <param name="reasonArgs">
179
+ /// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
180
+ /// </param>
181
+ public TObjectResultAssertion WithValue ( object expectedValue , string reason = "" , params object [ ] reasonArgs )
182
+ {
183
+ object actualValue = ObjectResultSubject . Value ;
184
+
185
+ Execute . Assertion
186
+ . BecauseOf ( reason , reasonArgs )
187
+ . ForCondition ( actualValue . IsSameOrEqualTo ( expectedValue ) )
188
+ . WithDefaultIdentifier ( Identifier + ".Value" )
189
+ . FailWith ( FailureMessages . CommonFailMessage , expectedValue , actualValue ) ;
190
+
191
+ return ( TObjectResultAssertion ) this ;
192
+ }
193
+
194
+ /// <summary>
195
+ /// Asserts that the <see cref="ObjectResult.Value"/> is equivalent to another object.
196
+ /// </summary>
197
+ /// <param name="expectation">The expected value.</param>
198
+ /// <param name="reason">
199
+ /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
200
+ /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
201
+ /// </param>
202
+ /// <param name="reasonArgs">
203
+ /// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
204
+ /// </param>
205
+ public TObjectResultAssertion WithValueEquivalentTo < TExpectation > ( TExpectation expectation ,
206
+ string reason = "" , params object [ ] reasonArgs )
207
+ {
208
+ return WithValueEquivalentTo ( expectation , config => config , reason , reasonArgs ) ;
209
+ }
210
+
211
+ /// <summary>
212
+ /// Asserts that the <see cref="ObjectResult.Value"/> is equivalent to another object.
213
+ /// </summary>
214
+ /// <param name="expectation">The expected status code.</param>
215
+ /// <param name="config">
216
+ /// A reference to the <see cref="EquivalencyAssertionOptions{TSubject}"/> configuration object that can be used
217
+ /// to influence the way the object graphs are compared. You can also provide an alternative instance of the
218
+ /// <see cref="EquivalencyAssertionOptions{TSubject}"/> class. The global defaults are determined by the
219
+ /// <see cref="AssertionOptions"/> class.
220
+ /// </param>
221
+ /// <param name="reason">
222
+ /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
223
+ /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
224
+ /// </param>
225
+ /// <param name="reasonArgs">
226
+ /// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
227
+ /// </param>
228
+ public TObjectResultAssertion WithValueEquivalentTo < TExpectation > ( TExpectation expectation ,
229
+ Func < EquivalencyAssertionOptions < TExpectation > , EquivalencyAssertionOptions < TExpectation > > config , string reason = "" , params object [ ] reasonArgs )
230
+ {
231
+ object actualValue = ObjectResultSubject . Value ;
232
+
233
+ actualValue . Should ( ) . BeEquivalentTo ( expectation , config , reason , reasonArgs ) ;
234
+
235
+ return ( TObjectResultAssertion ) this ;
236
+ }
237
+
238
+
239
+ /// <summary>
240
+ /// Asserts that the <see cref="ObjectResult.Value"/> statisfies the <paramref name="predicate"/>.
241
+ /// </summary>
242
+ /// <param name="predicate">
243
+ /// The predicate which must be satisfied by the <see cref="ObjectResult.Value"/>.
244
+ /// </param>
245
+ /// <param name="reason">
246
+ /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
247
+ /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
248
+ /// </param>
249
+ /// <param name="reasonArgs">
250
+ /// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
251
+ /// </param>
252
+ public TObjectResultAssertion WithValueMatch < TExpectation > ( Expression < Func < TExpectation , bool > > predicate ,
253
+ string reason = "" , params object [ ] reasonArgs )
254
+ {
255
+ object actualValue = ValueAs < TExpectation > ( ) ;
256
+
257
+ using ( var scope = new AssertionScope ( Identifier + ".Value" ) )
258
+ {
259
+ actualValue . Should ( ) . Match ( predicate , reason , reasonArgs ) ;
260
+ }
261
+
262
+ return ( TObjectResultAssertion ) this ;
263
+ }
168
264
169
265
/// <summary>
170
266
/// Asserts that the <see cref="ObjectResult.StatusCode"/> is the expected status code.
@@ -182,9 +278,9 @@ public TObjectResultAssertion WithStatusCode(int? expectedStatusCode, string rea
182
278
var actual = ObjectResultSubject . StatusCode ;
183
279
184
280
Execute . Assertion
281
+ . BecauseOf ( reason , reasonArgs )
185
282
. ForCondition ( expectedStatusCode == actual )
186
283
. WithDefaultIdentifier ( Identifier + ".StatusCode" )
187
- . BecauseOf ( reason , reasonArgs )
188
284
. FailWith ( FailureMessages . CommonFailMessage , expectedStatusCode , actual ) ;
189
285
190
286
return ( TObjectResultAssertion ) this ;
0 commit comments