예제 #1
0
        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);
        }
예제 #2
0
        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));
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }