@@ -248,9 +248,9 @@ public void CanValidateOptionsWithCustomError()
248
248
. Validate ( o => ! o . Boolean , "named Boolean must be false." ) ;
249
249
var sp = services . BuildServiceProvider ( ) ;
250
250
var error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptions < ComplexOptions > > ( ) . Value ) ;
251
- ValidateFailure < ComplexOptions > ( error , Options . DefaultName , "Boolean must be true." ) ;
251
+ ValidateFailure < ComplexOptions > ( error , Options . DefaultName , 1 , "Boolean must be true." ) ;
252
252
error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptionsMonitor < ComplexOptions > > ( ) . Get ( "named" ) ) ;
253
- ValidateFailure < ComplexOptions > ( error , "named" , "named Boolean must be false." ) ;
253
+ ValidateFailure < ComplexOptions > ( error , "named" , 1 , "named Boolean must be false." ) ;
254
254
}
255
255
256
256
@@ -281,7 +281,7 @@ public void CanValidateOptionsWithMultipleDefaultErrors()
281
281
282
282
var sp = services . BuildServiceProvider ( ) ;
283
283
var error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptions < ComplexOptions > > ( ) . Value ) ;
284
- ValidateFailure < ComplexOptions > ( error , Options . DefaultName , "A validation error has occured." , "A validation error has occured." ) ;
284
+ ValidateFailure < ComplexOptions > ( error , Options . DefaultName , 2 , "A validation error has occured." ) ;
285
285
}
286
286
287
287
[ Fact ]
@@ -301,7 +301,7 @@ public void CanValidateOptionsWithMixedOverloads()
301
301
302
302
var sp = services . BuildServiceProvider ( ) ;
303
303
var error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptions < ComplexOptions > > ( ) . Value ) ;
304
- ValidateFailure < ComplexOptions > ( error , Options . DefaultName , "A validation error has occured." , "Virtual" , "Integer" ) ;
304
+ ValidateFailure < ComplexOptions > ( error , Options . DefaultName , 3 , "A validation error has occured." , "Virtual" , "Integer" ) ;
305
305
}
306
306
307
307
public class BadValidator : IValidateOptions < FakeOptions >
@@ -360,10 +360,10 @@ public void CanValidateMultipleOptionsWithOneValidator()
360
360
361
361
var sp = services . BuildServiceProvider ( ) ;
362
362
var error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptions < ComplexOptions > > ( ) . Value ) ;
363
- ValidateFailure < ComplexOptions > ( error , Options . DefaultName , "Virtual != real" ) ;
363
+ ValidateFailure < ComplexOptions > ( error , Options . DefaultName , 1 , "Virtual != real" ) ;
364
364
365
365
error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptions < FakeOptions > > ( ) . Value ) ;
366
- ValidateFailure < FakeOptions > ( error , Options . DefaultName , "Message != real" ) ;
366
+ ValidateFailure < FakeOptions > ( error , Options . DefaultName , 1 , "Message != real" ) ;
367
367
368
368
var fake = sp . GetRequiredService < IOptionsMonitor < FakeOptions > > ( ) . Get ( "fake" ) ;
369
369
Assert . Equal ( "real" , fake . Message ) ;
@@ -387,15 +387,20 @@ public ValidateOptionsResult Validate(string name, ComplexOptions options)
387
387
}
388
388
}
389
389
390
- private void ValidateFailure < TOptions > ( OptionsValidationException e , string name = "" , params string [ ] errors )
390
+ private void ValidateFailure < TOptions > ( OptionsValidationException e , string name = "" , int count = 1 , params string [ ] errorsToMatch )
391
391
{
392
392
Assert . Equal ( typeof ( TOptions ) , e . OptionsType ) ;
393
393
Assert . Equal ( name , e . OptionsName ) ;
394
- if ( errors . Length == 0 )
394
+ if ( errorsToMatch . Length == 0 )
395
395
{
396
- errors = new string [ ] { "A validation error has occured." } ;
396
+ errorsToMatch = new string [ ] { "A validation error has occured." } ;
397
+ }
398
+ Assert . Equal ( count , e . Failures . Count ( ) ) ;
399
+ // Check for the error in any of the failures
400
+ foreach ( var error in errorsToMatch )
401
+ {
402
+ Assert . True ( e . Failures . FirstOrDefault ( f => f . Contains ( error ) ) != null , "Did not find: " + error ) ;
397
403
}
398
- Assert . True ( errors . SequenceEqual ( e . Failures ) , "Expected: " + String . Join ( " - " , e . Failures ) ) ;
399
404
}
400
405
401
406
[ Fact ]
@@ -415,13 +420,13 @@ public void CanValidateOptionsThatDependOnOptions()
415
420
var sp = services . BuildServiceProvider ( ) ;
416
421
417
422
var error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptions < ComplexOptions > > ( ) . Value ) ;
418
- ValidateFailure < ComplexOptions > ( error , Options . DefaultName , "Virtual != target" ) ;
423
+ ValidateFailure < ComplexOptions > ( error , Options . DefaultName , 1 , "Virtual != target" ) ;
419
424
420
425
error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptionsMonitor < ComplexOptions > > ( ) . Get ( Options . DefaultName ) ) ;
421
- ValidateFailure < ComplexOptions > ( error , Options . DefaultName , "Virtual != target" ) ;
426
+ ValidateFailure < ComplexOptions > ( error , Options . DefaultName , 1 , "Virtual != target" ) ;
422
427
423
428
error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptionsMonitor < ComplexOptions > > ( ) . Get ( "no" ) ) ;
424
- ValidateFailure < ComplexOptions > ( error , "no" , "Virtual != target" ) ;
429
+ ValidateFailure < ComplexOptions > ( error , "no" , 1 , "Virtual != target" ) ;
425
430
426
431
var op = sp . GetRequiredService < IOptionsMonitor < ComplexOptions > > ( ) . Get ( "yes" ) ;
427
432
Assert . Equal ( "target" , op . Virtual ) ;
@@ -513,7 +518,7 @@ public void CanValidateOptionsEagerly()
513
518
var startupValidator = sp . GetRequiredService < IStartupValidator > ( ) ;
514
519
515
520
var error = Assert . Throws < OptionsValidationException > ( ( ) => startupValidator . Validate ( ) ) ;
516
- ValidateFailure < ComplexOptions > ( error , Options . DefaultName , "A validation error has occured." , "Virtual" , "Integer" ) ;
521
+ ValidateFailure < ComplexOptions > ( error , Options . DefaultName , 3 , "A validation error has occured." , "Virtual" , "Integer" ) ;
517
522
}
518
523
519
524
[ AttributeUsage ( AttributeTargets . Field | AttributeTargets . Property , AllowMultiple = false , Inherited = true ) ]
@@ -580,12 +585,12 @@ public void CanValidateDataAnnotations()
580
585
var sp = services . BuildServiceProvider ( ) ;
581
586
582
587
var error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptions < AnnotatedOptions > > ( ) . Value ) ;
583
- ValidateFailure < AnnotatedOptions > ( error , Options . DefaultName ,
584
- @ "DataAnnotation validation failed for members Required with the error 'The Required field is required.'.
585
- DataAnnotation validation failed for members StringLength with the error 'Too long.'.
586
- DataAnnotation validation failed for members IntRange with the error 'Out of range.'.
587
- DataAnnotation validation failed for members Custom with the error 'The field Custom is invalid.'.
588
- DataAnnotation validation failed for members Dep1, Dep2 with the error 'Dep1 != Dep2'." ) ;
588
+ ValidateFailure < AnnotatedOptions > ( error , Options . DefaultName , 1 ,
589
+ "DataAnnotation validation failed for members Required with the error 'The Required field is required.'." ,
590
+ " DataAnnotation validation failed for members StringLength with the error 'Too long.'." ,
591
+ " DataAnnotation validation failed for members IntRange with the error 'Out of range.'." ,
592
+ " DataAnnotation validation failed for members Custom with the error 'The field Custom is invalid.'." ,
593
+ " DataAnnotation validation failed for members Dep1, Dep2 with the error 'Dep1 != Dep2'.") ;
589
594
}
590
595
591
596
[ Fact ]
@@ -606,15 +611,13 @@ public void CanValidateMixDataAnnotations()
606
611
var sp = services . BuildServiceProvider ( ) ;
607
612
608
613
var error = Assert . Throws < OptionsValidationException > ( ( ) => sp . GetRequiredService < IOptions < AnnotatedOptions > > ( ) . Value ) ;
609
- ValidateFailure < AnnotatedOptions > ( error , Options . DefaultName ,
610
- @ "DataAnnotation validation failed for members Required with the error 'The Required field is required.'.
611
- DataAnnotation validation failed for members StringLength with the error 'Too long.'.
612
- DataAnnotation validation failed for members IntRange with the error 'Out of range.'.
613
- DataAnnotation validation failed for members Custom with the error 'The field Custom is invalid.'.
614
- DataAnnotation validation failed for members Dep1, Dep2 with the error 'Dep1 != Dep2'." ,
614
+ ValidateFailure < AnnotatedOptions > ( error , Options . DefaultName , 2 ,
615
+ "DataAnnotation validation failed for members Required with the error 'The Required field is required.'." ,
616
+ " DataAnnotation validation failed for members StringLength with the error 'Too long.'." ,
617
+ " DataAnnotation validation failed for members IntRange with the error 'Out of range.'." ,
618
+ " DataAnnotation validation failed for members Custom with the error 'The field Custom is invalid.'." ,
619
+ " DataAnnotation validation failed for members Dep1, Dep2 with the error 'Dep1 != Dep2'.",
615
620
"I don't want to go to nowhere!" ) ;
616
621
}
617
-
618
-
619
622
}
620
623
}
0 commit comments