@@ -22,49 +22,46 @@ public class BindModelAsync
22
22
public void Should_try_bind_enum_when_value_is_null_or_default ( )
23
23
{
24
24
// class - int
25
- Bind < SmartEnum_IntBased > ( null ) . Should ( ) . Be ( null ) ;
25
+ BindNot < SmartEnum_IntBased > ( null ) ;
26
26
FluentActions . Invoking ( ( ) => Bind < SmartEnum_IntBased > ( "0" ) ) . Should ( ) . Throw < Exception > ( ) . WithMessage ( "There is no item of type 'SmartEnum_IntBased' with the identifier '0'." ) ;
27
27
28
28
// class - string
29
- Bind < SmartEnum_StringBased > ( null ) . Should ( ) . Be ( null ) ;
29
+ BindNot < SmartEnum_StringBased > ( null ) ;
30
30
31
31
// class - class
32
- Bind < SmartEnum_ClassBased > ( null ) . Should ( ) . Be ( null ) ;
32
+ BindNot < SmartEnum_ClassBased > ( null ) ;
33
33
}
34
34
35
35
[ Fact ]
36
36
public void Should_try_bind_keyed_value_object_when_value_is_null_or_default ( )
37
37
{
38
38
// class - int
39
- Bind < IntBasedReferenceValueObject > ( null ) . Should ( ) . Be ( null ) ;
39
+ BindNot < IntBasedReferenceValueObject > ( null ) ;
40
40
41
41
// class - string
42
- Bind < StringBasedReferenceValueObject > ( null ) . Should ( ) . Be ( null ) ;
42
+ BindNot < StringBasedReferenceValueObject > ( null ) ;
43
43
44
44
// class - class
45
- Bind < ClassBasedReferenceValueObject > ( null ) . Should ( ) . Be ( null ) ;
45
+ BindNot < ClassBasedReferenceValueObject > ( null ) ;
46
46
47
47
// nullable struct - int
48
- Bind < IntBasedStructValueObject ? > ( null ) . Should ( ) . Be ( null ) ;
48
+ BindNot < IntBasedStructValueObject ? > ( null ) ;
49
49
Bind < IntBasedStructValueObject ? > ( "0" ) . Should ( ) . Be ( IntBasedStructValueObject . Create ( 0 ) ) ;
50
50
51
51
// struct - int
52
52
Bind < IntBasedStructValueObject > ( "0" ) . Should ( ) . Be ( IntBasedStructValueObject . Create ( 0 ) ) ; // AllowDefaultStructs = true
53
- FluentActions . Invoking ( ( ) => Bind < IntBasedStructValueObject > ( null ) )
54
- . Should ( ) . Throw < Exception > ( ) . WithMessage ( "Cannot convert null to type \" IntBasedStructValueObject\" ." ) ;
53
+ BindNot < IntBasedStructValueObject > ( null ) ;
55
54
56
55
Bind < IntBasedStructValueObjectDoesNotAllowDefaultStructs > ( "0" ) . Should ( ) . Be ( IntBasedStructValueObjectDoesNotAllowDefaultStructs . Create ( 0 ) ) ; // AllowDefaultStructs = true
57
56
58
57
// nullable struct - string
59
- Bind < StringBasedStructValueObject ? > ( null ) . Should ( ) . Be ( null ) ;
58
+ BindNot < StringBasedStructValueObject ? > ( null ) ;
60
59
61
60
// struct - string
62
- FluentActions . Invoking ( ( ) => Bind < StringBasedStructValueObject > ( null ) ) // AllowDefaultStructs = false
63
- . Should ( ) . Throw < Exception > ( ) . WithMessage ( "Cannot convert null to type \" StringBasedStructValueObject\" because it doesn't allow default values." ) ;
61
+ BindNot < StringBasedStructValueObject > ( null ) ; // AllowDefaultStructs = false;
64
62
65
63
// struct - class
66
- FluentActions . Invoking ( ( ) => Bind < ReferenceTypeBasedStructValueObjectDoesNotAllowDefaultStructs > ( null ) ) // AllowDefaultStructs = false
67
- . Should ( ) . Throw < Exception > ( ) . WithMessage ( "Cannot convert a string to type \" ReferenceTypeBasedStructValueObjectDoesNotAllowDefaultStructs\" ." ) ;
64
+ BindNot < ReferenceTypeBasedStructValueObjectDoesNotAllowDefaultStructs > ( null ) ; // AllowDefaultStructs = false
68
65
}
69
66
70
67
[ Fact ]
@@ -137,12 +134,13 @@ public async Task Should_bind_string_based_value_type_with_NullInFactoryMethodsY
137
134
}
138
135
139
136
[ Fact ]
140
- public async Task Should_bind_null_with_NullInFactoryMethodsYieldsNull ( )
137
+ public async Task Should_not_set_model_when_trying_to_bind_null ( )
141
138
{
139
+ // ASP.NET or rather IQueryCollection doesn't distinguish between null and none
142
140
var ctx = await BindAsync < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull > ( null ) ;
143
141
144
142
ctx . ModelState . ErrorCount . Should ( ) . Be ( 0 ) ;
145
- ctx . Result . IsModelSet . Should ( ) . BeTrue ( ) ;
143
+ ctx . Result . IsModelSet . Should ( ) . BeFalse ( ) ;
146
144
ctx . Result . Model . Should ( ) . BeNull ( ) ;
147
145
}
148
146
@@ -179,10 +177,11 @@ public async Task Should_bind_string_based_value_type_with_StringBasedReferenceV
179
177
[ Fact ]
180
178
public async Task Should_bind_null_with_StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull ( )
181
179
{
180
+ // ASP.NET or rather IQueryCollection doesn't distinguish between null and none
182
181
var ctx = await BindAsync < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull > ( null ) ;
183
182
184
183
ctx . ModelState . ErrorCount . Should ( ) . Be ( 0 ) ;
185
- ctx . Result . IsModelSet . Should ( ) . BeTrue ( ) ;
184
+ ctx . Result . IsModelSet . Should ( ) . BeFalse ( ) ;
186
185
ctx . Result . Model . Should ( ) . BeNull ( ) ;
187
186
}
188
187
@@ -299,6 +298,21 @@ private static T Bind<T>(string value)
299
298
return ( T ) context . Result . Model ;
300
299
}
301
300
301
+ private static void BindNot < T > ( string value )
302
+ {
303
+ // ASP.NET or rather IQueryCollection doesn't distinguish between null and none
304
+ var context = BindAsync < T > ( value ) . GetAwaiter ( ) . GetResult ( ) ;
305
+
306
+ if ( ! context . ModelState . IsValid )
307
+ throw new Exception ( context . ModelState [ "name" ] ! . Errors [ 0 ] . ErrorMessage ) ;
308
+
309
+ if ( context . Result . IsModelSet )
310
+ throw new Exception ( "Model msut not be set" ) ;
311
+
312
+ if ( context . Result . Model is not null )
313
+ throw new Exception ( "Model must be null" ) ;
314
+ }
315
+
302
316
private static async Task < DefaultModelBindingContext > BindAsync < T > ( string value )
303
317
{
304
318
var query = new Dictionary < string , StringValues > { { "name" , value } } ;
0 commit comments