Skip to content

Commit

Permalink
Fix assertions for With/Without test methods when there are multiple …
Browse files Browse the repository at this point in the history
…failures (#1937) (#1938)

* Apply validation predicate to matched entries only for Without*** worked correctly when there is more than one failure
* Apply validation predicate only to matched entries for With*** assertions too to correctly display failures
  • Loading branch information
Aleksei-Pankratev-EPAM committed May 7, 2022
1 parent f3cad58 commit eae767d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
51 changes: 51 additions & 0 deletions src/FluentValidation.Tests/ValidatorTesterTester.cs
Expand Up @@ -469,6 +469,57 @@ public class ValidatorTesterTester {
ex.Message.ShouldEqual("Found an unexpected error code of 'bar'");
}

[Fact]
public void Expected_without_error_code_check() {
//#1937
var validator = new InlineValidator<Person> {
v => v.RuleFor(x => x.Surname).NotNull(),
v => v.RuleFor(x => x.Forename).NotNull()
};

validator.TestValidate(new Person())
.ShouldHaveValidationErrorFor(x => x.Surname)
.WithoutErrorCode("foo")
.WithoutErrorMessage("bar")
.WithoutSeverity(Severity.Warning)
.WithoutCustomState(1);
}

[Fact]
public void Unexpected_with_error_message_check() {
//#1937
var validator = new InlineValidator<Person>
{
v => v.RuleFor(x => x.Forename).NotEmpty(),
v => v.RuleFor(x => x.Surname).NotEmpty()
};

var ex = Assert.Throws<ValidationTestException>(() =>
validator.TestValidate(new Person())
.ShouldHaveValidationErrorFor(x => x.Surname)
.WithErrorMessage("bar"));
ex.Message.ShouldEqual("Expected an error message of 'bar'. Actual message was ''Surname' must not be empty.'");
}

[Fact]
public void Expected_with_error_code_check() {
var validator = new InlineValidator<Person> {
v => v.RuleFor(x => x.Forename).NotNull(),
v => v.RuleFor(x => x.Surname).NotNull()
.WithErrorCode("foo")
.WithMessage("bar")
.WithSeverity(Severity.Warning)
.WithState(_ => 1)
};

validator.TestValidate(new Person())
.ShouldHaveValidationErrorFor(x => x.Surname)
.WithErrorCode("foo")
.WithErrorMessage("bar")
.WithSeverity(Severity.Warning)
.WithCustomState(1);
}

[Fact]
public void Expected_severity_check() {
var validator = new InlineValidator<Person> {
Expand Down
4 changes: 2 additions & 2 deletions src/FluentValidation/TestHelper/ValidatorTestExtensions.cs
Expand Up @@ -184,7 +184,7 @@ public static class ValidationTestExtension {
}

public static ITestValidationWith When(this ITestValidationContinuation failures, Func<ValidationFailure, bool> failurePredicate, string exceptionMessage = null) {
var result = TestValidationContinuation.Create(failures);
var result = TestValidationContinuation.Create(((TestValidationContinuation)failures).MatchedFailures);
result.ApplyPredicate(failurePredicate);

var anyMatched = result.Any();
Expand All @@ -198,7 +198,7 @@ public static class ValidationTestExtension {
}

public static ITestValidationContinuation WhenAll(this ITestValidationContinuation failures, Func<ValidationFailure, bool> failurePredicate, string exceptionMessage = null) {
var result = TestValidationContinuation.Create(failures);
var result = TestValidationContinuation.Create(((TestValidationContinuation)failures).MatchedFailures);
result.ApplyPredicate(failurePredicate);

bool allMatched = !result.UnmatchedFailures.Any();
Expand Down

0 comments on commit eae767d

Please sign in to comment.