11namespace MyTested . AspNetCore . Mvc . Test . BuildersTests . ActionResultsTests . BadRequestTests
22{
3+ using Exceptions ;
34 using Microsoft . AspNetCore . Mvc . ModelBinding ;
45 using Setups ;
56 using Setups . Controllers ;
67 using Setups . Models ;
78 using Xunit ;
8-
9+
910 public class BadRequestTestBuilderTests
1011 {
1112 [ Fact ]
@@ -21,16 +22,48 @@ public void WithModelStateForShouldNotThrowExceptionWhenModelStateHasSameErrors(
2122 . Calling ( c => c . BadRequestWithModelState ( requestModelWithErrors ) )
2223 . ShouldReturn ( )
2324 . 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.'." ) ;
3467 }
3568 }
3669}
0 commit comments