1
1
namespace MyTested . AspNetCore . Mvc . Test . BuildersTests . ActionResultsTests . BadRequestTests
2
2
{
3
+ using Exceptions ;
3
4
using Microsoft . AspNetCore . Mvc . ModelBinding ;
4
5
using Setups ;
5
6
using Setups . Controllers ;
6
7
using Setups . Models ;
7
8
using Xunit ;
8
-
9
+
9
10
public class BadRequestTestBuilderTests
10
11
{
11
12
[ Fact ]
@@ -21,16 +22,48 @@ public void WithModelStateForShouldNotThrowExceptionWhenModelStateHasSameErrors(
21
22
. Calling ( c => c . BadRequestWithModelState ( requestModelWithErrors ) )
22
23
. ShouldReturn ( )
23
24
. BadRequest ( )
24
- . WithModelStateErrorFor < RequestModel > ( )
25
- . ContainingErrorFor ( m => m . Integer ) . ThatEquals ( "The field Integer must be between 1 and 2147483647." )
26
- . AndAlso ( )
27
- . ContainingErrorFor ( m => m . RequiredString ) . BeginningWith ( "The RequiredString" )
28
- . AndAlso ( )
29
- . ContainingErrorFor ( m => m . RequiredString ) . EndingWith ( "required." )
30
- . AndAlso ( )
31
- . ContainingErrorFor ( m => m . RequiredString ) . Containing ( "field" )
32
- . AndAlso ( )
33
- . ContainingNoErrorFor ( m => m . NonRequiredString ) ;
25
+ . WithModelStateError ( modelStateError => modelStateError
26
+ . For < RequestModel > ( )
27
+ . ContainingErrorFor ( m => m . Integer ) . ThatEquals ( "The field Integer must be between 1 and 2147483647." )
28
+ . AndAlso ( )
29
+ . ContainingErrorFor ( m => m . RequiredString ) . BeginningWith ( "The RequiredString" )
30
+ . AndAlso ( )
31
+ . ContainingErrorFor ( m => m . RequiredString ) . EndingWith ( "required." )
32
+ . AndAlso ( )
33
+ . ContainingErrorFor ( m => m . RequiredString ) . Containing ( "field" )
34
+ . AndAlso ( )
35
+ . ContainingNoErrorFor ( m => m . NonRequiredString ) ) ;
36
+ }
37
+
38
+ [ Fact ]
39
+ public void WithModelStateForShouldThrowExceptionWhenModelStateHasSameErrors ( )
40
+ {
41
+ Test . AssertException < ModelErrorAssertionException > (
42
+ ( ) =>
43
+ {
44
+ var requestModelWithErrors = TestObjectFactory . GetRequestModelWithErrors ( ) ;
45
+ var modelState = new ModelStateDictionary ( ) ;
46
+ modelState . AddModelError ( "Integer" , "The field Integer must be between 1 and 2147483647." ) ;
47
+ modelState . AddModelError ( "RequiredString" , "The RequiredString field is required." ) ;
48
+
49
+ MyController < MvcController >
50
+ . Instance ( )
51
+ . Calling ( c => c . BadRequestWithModelState ( requestModelWithErrors ) )
52
+ . ShouldReturn ( )
53
+ . BadRequest ( )
54
+ . WithModelStateError ( modelStateError => modelStateError
55
+ . For < RequestModel > ( )
56
+ . ContainingErrorFor ( m => m . Integer ) . ThatEquals ( "The field Integer 1 must be between 1 and 2147483647." )
57
+ . AndAlso ( )
58
+ . ContainingErrorFor ( m => m . RequiredString ) . BeginningWith ( "The RequiredString" )
59
+ . AndAlso ( )
60
+ . ContainingErrorFor ( m => m . RequiredString ) . EndingWith ( "required." )
61
+ . AndAlso ( )
62
+ . ContainingErrorFor ( m => m . RequiredString ) . Containing ( "field" )
63
+ . AndAlso ( )
64
+ . ContainingNoErrorFor ( m => m . NonRequiredString ) ) ;
65
+ } ,
66
+ "When calling BadRequestWithModelState action in MvcController expected error message for key Integer to be 'The field Integer 1 must be between 1 and 2147483647.', but instead found 'The field Integer must be between 1 and 2147483647.'." ) ;
34
67
}
35
68
}
36
69
}
0 commit comments