internal static ITestValidationWith ShouldHaveValidationError(IList <ValidationFailure> errors, string propertyName, bool shouldNormalizePropertyName) { var result = TestValidationContinuation.Create(errors); result.ApplyPredicate(x => (shouldNormalizePropertyName ? NormalizePropertyName(x.PropertyName) == propertyName : x.PropertyName == propertyName) || (string.IsNullOrEmpty(x.PropertyName) && string.IsNullOrEmpty(propertyName)) || propertyName == MatchAnyFailure); if (result.Any()) { return(result); } // We expected an error but failed to match it. var errorMessageBanner = $"Expected a validation error for property {propertyName}"; string errorMessage = ""; if (errors?.Any() == true) { string errorMessageDetails = ""; for (int i = 0; i < errors.Count; i++) { errorMessageDetails += $"[{i}]: {errors[i].PropertyName}\n"; } errorMessage = $"{errorMessageBanner}\n----\nProperties with Validation Errors:\n{errorMessageDetails}"; } else { errorMessage = $"{errorMessageBanner}"; } throw new ValidationTestException(errorMessage); }
public static ITestValidationContinuation ShouldHaveAnyValidationError <T>(this TestValidationResult <T> testValidationResult) { if (!testValidationResult.Errors.Any()) { throw new ValidationTestException($"Expected at least one validation error, but none were found."); } return(TestValidationContinuation.Create(testValidationResult.Errors)); }
public static TestValidationContinuation Create(ITestValidationContinuation continuation) { if (continuation is TestValidationContinuation instance) { return(instance); } var allFailures = continuation.Union(continuation.UnmatchedFailures); instance = new TestValidationContinuation(allFailures); instance.ApplyPredicate(failure => !continuation.UnmatchedFailures.Contains(failure)); return(instance); }
public static ITestValidationContinuation WhenAll(this ITestValidationContinuation failures, Func <ValidationFailure, bool> failurePredicate, string exceptionMessage = null) { var result = TestValidationContinuation.Create(((TestValidationContinuation)failures).MatchedFailures); result.ApplyPredicate(failurePredicate); bool allMatched = !result.UnmatchedFailures.Any(); if (!allMatched) { var failure = result.UnmatchedFailures.First(); string message = BuildErrorMessage(failure, exceptionMessage, "Found an unexpected validation error"); throw new ValidationTestException(message); } return(result); }
public static ITestValidationWith When(this ITestValidationContinuation failures, Func <ValidationFailure, bool> failurePredicate, string exceptionMessage = null) { var result = TestValidationContinuation.Create(((TestValidationContinuation)failures).MatchedFailures); result.ApplyPredicate(failurePredicate); var anyMatched = result.Any(); if (!anyMatched) { var failure = result.UnmatchedFailures.FirstOrDefault(); string message = BuildErrorMessage(failure, exceptionMessage, "Expected validation error was not found"); throw new ValidationTestException(message); } return(result); }