Skip to content
This repository was archived by the owner on Nov 7, 2018. It is now read-only.

Commit 0caa14d

Browse files
authored
Search for errors instead of expecting strict match (#275)
1 parent 6127812 commit 0caa14d

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

test/Microsoft.Extensions.Options.Test/OptionsBuilderTest.cs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ public void CanValidateOptionsWithCustomError()
248248
.Validate(o => !o.Boolean, "named Boolean must be false.");
249249
var sp = services.BuildServiceProvider();
250250
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.");
252252
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.");
254254
}
255255

256256

@@ -281,7 +281,7 @@ public void CanValidateOptionsWithMultipleDefaultErrors()
281281

282282
var sp = services.BuildServiceProvider();
283283
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.");
285285
}
286286

287287
[Fact]
@@ -301,7 +301,7 @@ public void CanValidateOptionsWithMixedOverloads()
301301

302302
var sp = services.BuildServiceProvider();
303303
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");
305305
}
306306

307307
public class BadValidator : IValidateOptions<FakeOptions>
@@ -360,10 +360,10 @@ public void CanValidateMultipleOptionsWithOneValidator()
360360

361361
var sp = services.BuildServiceProvider();
362362
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");
364364

365365
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");
367367

368368
var fake = sp.GetRequiredService<IOptionsMonitor<FakeOptions>>().Get("fake");
369369
Assert.Equal("real", fake.Message);
@@ -387,15 +387,20 @@ public ValidateOptionsResult Validate(string name, ComplexOptions options)
387387
}
388388
}
389389

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)
391391
{
392392
Assert.Equal(typeof(TOptions), e.OptionsType);
393393
Assert.Equal(name, e.OptionsName);
394-
if (errors.Length == 0)
394+
if (errorsToMatch.Length == 0)
395395
{
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);
397403
}
398-
Assert.True(errors.SequenceEqual(e.Failures), "Expected: " + String.Join(" - ", e.Failures));
399404
}
400405

401406
[Fact]
@@ -415,13 +420,13 @@ public void CanValidateOptionsThatDependOnOptions()
415420
var sp = services.BuildServiceProvider();
416421

417422
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");
419424

420425
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");
422427

423428
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");
425430

426431
var op = sp.GetRequiredService<IOptionsMonitor<ComplexOptions>>().Get("yes");
427432
Assert.Equal("target", op.Virtual);
@@ -513,7 +518,7 @@ public void CanValidateOptionsEagerly()
513518
var startupValidator = sp.GetRequiredService<IStartupValidator>();
514519

515520
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");
517522
}
518523

519524
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
@@ -580,12 +585,12 @@ public void CanValidateDataAnnotations()
580585
var sp = services.BuildServiceProvider();
581586

582587
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'.");
589594
}
590595

591596
[Fact]
@@ -606,15 +611,13 @@ public void CanValidateMixDataAnnotations()
606611
var sp = services.BuildServiceProvider();
607612

608613
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'.",
615620
"I don't want to go to nowhere!");
616621
}
617-
618-
619622
}
620623
}

0 commit comments

Comments
 (0)