public void ShouldHaveValidationErrorFor_Only()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Surname).Must((x, ct) => false);
            validator.TestValidate(new Person())
            .ShouldHaveValidationErrorFor(x => x.Surname)
            .Only();
        }
Beispiel #2
0
        public void Matches_any_failure()
        {
            var validator = new InlineValidator <Person> {
                v => v.RuleFor(x => x.Surname).NotEqual("foo"),
            };

            var resultWithFailure = validator.TestValidate(new Person {
                Surname = "foo"
            });
            var resultWithoutFailure = validator.TestValidate(new Person {
                Surname = ""
            });

            Assert.Throws <ValidationTestException>(() => resultWithoutFailure.ShouldHaveAnyValidationError());
            Assert.Throws <ValidationTestException>(() => resultWithFailure.ShouldNotHaveAnyValidationErrors());

            // Neither should throw.
            resultWithFailure.ShouldHaveAnyValidationError();
            resultWithoutFailure.ShouldNotHaveAnyValidationErrors();
        }
        public void ShouldHaveValidationErrorFor_WithPropertyName_Only()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => DateTime.Now)
            .Must((x, ct) => false);
            validator.TestValidate(new Person())
            .ShouldHaveValidationErrorFor("Now")
            .WithErrorMessage("The specified condition was not met for 'Now'.")
            .Only();
        }
        public void ShouldHaveValidationErrorFor_Only_throws()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Surname).Must((x, ct) => false);
            validator.RuleFor(x => x.Age).Must((x, ct) => false);
            Assert.Throws <ValidationTestException>(() =>
                                                    validator.TestValidate(new Person())
                                                    .ShouldHaveValidationErrorFor(x => x.Surname)
                                                    .Only()
                                                    ).Message.ShouldEqual("Expected to have errors only matching specified conditions\n----\nUnexpected Errors:\n[0]: The specified condition was not met for 'Age'.\n");
        }
        public void Matches_model_level_rule()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x).Must(x => false);
            validator.RuleFor(x => x.Surname).Must(x => false);

            var result = validator.TestValidate(new Person());

            result.ShouldHaveValidationErrorFor(x => x);
            result.ShouldHaveValidationErrorFor("");
        }
        public void ShouldHaveValidationErrorFor_WithErrorCode_Only()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Surname)
            .Must((x, ct) => false).WithErrorCode("100")
            .NotEmpty().WithErrorCode("100");
            validator.TestValidate(new Person())
            .ShouldHaveValidationErrorFor(x => x.Surname)
            .WithErrorCode("100")
            .Only();
        }
        public void ShouldHaveValidationErrorFor_WithSeverity_Only()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Surname)
            .Must((x, ct) => false).WithSeverity(Severity.Warning)
            .NotEmpty().WithSeverity(Severity.Warning);
            validator.TestValidate(new Person())
            .ShouldHaveValidationErrorFor(x => x.Surname)
            .WithSeverity(Severity.Warning)
            .Only();
        }
        public void Expected_error_code_check()
        {
            var validator = new InlineValidator <Person> {
                v => v.RuleFor(x => x.Surname).NotNull().WithErrorCode("bar")
            };
            var ex = Assert.Throws <ValidationTestException>(() =>
                                                             validator.TestValidate(new Person())
                                                             .ShouldHaveValidationErrorFor(x => x.Surname)
                                                             .WithErrorCode("foo"));

            ex.Message.ShouldEqual("Expected an error code of 'foo'. Actual error code was 'bar'");
        }
        public void ShouldHaveValidationErrorFor_WithMessage_Only()
        {
            var validator = new InlineValidator <Person>();
            var message   = "Something's wrong but I won't tell you what";

            validator.RuleFor(x => x.Surname)
            .Must((x, ct) => false).WithMessage(message)
            .NotEmpty().WithMessage(message);
            validator.TestValidate(new Person())
            .ShouldHaveValidationErrorFor(x => x.Surname)
            .WithErrorMessage(message)
            .Only();
        }
        public void Unexpected_state_check()
        {
            var validator = new InlineValidator <Person> {
                v => v.RuleFor(x => x.Surname).NotNull().WithState(x => "bar"),
                v => v.RuleFor(x => x.Surname).NotNull().WithState(x => "foo"),
            };
            var ex = Assert.Throws <ValidationTestException>(() =>
                                                             validator.TestValidate(new Person())
                                                             .ShouldHaveValidationErrorFor(x => x.Surname)
                                                             .WithoutCustomState("bar"));

            ex.Message.ShouldEqual("Found an unexpected custom state of 'bar'");
        }
        public void Unexpected_severity_check()
        {
            var validator = new InlineValidator <Person> {
                v => v.RuleFor(x => x.Surname).NotNull().WithSeverity(Severity.Warning),
                v => v.RuleFor(x => x.Surname).NotNull().WithSeverity(Severity.Error),
            };
            var ex = Assert.Throws <ValidationTestException>(() =>
                                                             validator.TestValidate(new Person())
                                                             .ShouldHaveValidationErrorFor(x => x.Surname)
                                                             .WithoutSeverity(Severity.Warning));

            ex.Message.ShouldEqual($"Found an unexpected severity of '{nameof(Severity.Warning)}'");
        }
        public void ShouldHaveValidationErrorFor_WithErrorCode_Only_throws()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Surname)
            .Must((x, ct) => false).WithErrorCode("100")
            .NotEmpty().WithErrorCode("200");
            Assert.Throws <ValidationTestException>(() =>
                                                    validator.TestValidate(new Person())
                                                    .ShouldHaveValidationErrorFor(x => x.Surname)
                                                    .WithErrorCode("100")
                                                    .Only()
                                                    ).Message.ShouldEqual("Expected to have errors only matching specified conditions\n----\nUnexpected Errors:\n[0]: 'Surname' must not be empty.\n");
        }
        public void ShouldHaveValidationErrorFor_WithPropertyName_Only_throws()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => DateTime.Now)
            .Must((x, ct) => false)
            .LessThan(new DateTime(1900, 1, 1));
            Assert.Throws <ValidationTestException>(() =>
                                                    validator.TestValidate(new Person())
                                                    .ShouldHaveValidationErrorFor("Now")
                                                    .WithErrorMessage("The specified condition was not met for 'Now'.")
                                                    .Only()
                                                    ).Message.ShouldEqual("Expected to have errors only matching specified conditions\n----\nUnexpected Errors:\n[0]: 'Now' must be less than '1/1/1900 12:00:00 AM'.\n");
        }
        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);
        }
Beispiel #15
0
        public void Model_level_check_fails_if_no_model_level_failures()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Surname).Must(x => false);
            var result = validator.TestValidate(new Person());

            Assert.Throws <ValidationTestException>(() => {
                result.ShouldHaveValidationErrorFor(x => x);
            });

            Assert.Throws <ValidationTestException>(() => {
                result.ShouldHaveValidationErrorFor("");
            });
        }
        public void Unexpected_message_check(string withoutErrMsg, string[] errMessages)
        {
            var validator = new InlineValidator <Person>();

            foreach (var msg in errMessages)
            {
                validator.Add(v => v.RuleFor(x => x.Surname).NotNull().WithMessage(msg));
            }

            var ex = Assert.Throws <ValidationTestException>(() =>
                                                             validator.TestValidate(new Person {
            }).ShouldHaveAnyValidationError().WithoutErrorMessage(withoutErrMsg));

            ex.Message.ShouldEqual($"Found an unexpected error message of '{withoutErrMsg}'");
        }
        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.'");
        }
Beispiel #18
0
        public void Allows_only_one_failure_to_match()
        {
            var validator = new InlineValidator <Person> {
                v => v.RuleFor(x => x.Surname).Equal("a").WithErrorCode("nota"),
                v => v.RuleFor(x => x.Surname).Equal("b").WithErrorCode("notb")
            };

            var person = new Person()
            {
                Surname = "c"
            };
            var result = validator.TestValidate(person);

            result.ShouldHaveError().WithErrorCode("nota");
            result.ShouldHaveError().WithErrorCode("notb");
        }
        public void Custom_state_comparer_check()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Surname).NotNull().WithState(x => "Test" + 123);
            var result = validator.TestValidate(new Person());

            // Throws without comparer.
            Assert.Throws <ValidationTestException>(() => {
                result.ShouldHaveValidationErrorFor(x => x.Surname)
                .WithCustomState("test123");
            });

            // Doesn't throw with comparer.
            result.ShouldHaveValidationErrorFor(x => x.Surname)
            .WithCustomState("test123", StringComparer.OrdinalIgnoreCase);
        }
Beispiel #20
0
        public void Can_use_indexer_in_string_message_inverse()
        {
            var validator      = new InlineValidator <Person>();
            var orderValidator = new InlineValidator <Order>();

            orderValidator.RuleFor(x => x.ProductName).Null();
            validator.RuleForEach(x => x.Orders).SetValidator(orderValidator);

            var model = new Person {
                Orders = new List <Order> {
                    new Order()
                }
            };
            var result = validator.TestValidate(model);

            result.ShouldNotHaveValidationErrorFor("Orders[0].ProductName");
        }
        public void Expected_message_argument_check()
        {
            var validator = new InlineValidator <Person> {
                v => v.RuleFor(x => x.Surname)
                .Must((x, y, context) => {
                    context.MessageFormatter.AppendArgument("Foo", "bar");
                    return(false);
                })
                .WithMessage("{Foo}")
            };
            var ex = Assert.Throws <ValidationTestException>(() =>
                                                             validator.TestValidate(new Person())
                                                             .ShouldHaveValidationErrorFor(x => x.Surname)
                                                             .WithMessageArgument("Foo", "foo")
                                                             );

            ex.Message.ShouldEqual("Expected message argument 'Foo' with value 'foo'. Actual value was 'bar'");
        }
        public void Test_custom_state_with_concatenated_string()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Surname).NotNull().WithState(x => "Test" + 123);
            var result = validator.TestValidate(new Person());

            // String concatenated with integer means a different string reference is created:

            /*
             *      object s1 = "Test" + 123.ToString();
             *      object s2 = "Test123";
             *      bool check1 = s1 == s2; // False
             */
            // Test to ensure that this scenario is handled properly.
            result.ShouldHaveValidationErrorFor(x => x.Surname)
            .WithCustomState("Test123");
        }
        public void Expected_error_code_check()
        {
            bool exceptionCaught = false;

            try {
                var validator = new InlineValidator <Person> {
                    v => v.RuleFor(x => x.Surname).NotNull().WithErrorCode("bar")
                };
                validator.TestValidate(new Person())
                .ShouldHaveValidationErrorFor(x => x.Surname)
                .WithErrorCode("foo");
            }
            catch (ValidationTestException e) {
                exceptionCaught = true;

                e.Message.ShouldEqual("Expected an error code of 'foo'. Actual error code was 'bar'");
            }

            exceptionCaught.ShouldBeTrue();
        }
        public void Expected_severity_check()
        {
            bool exceptionCaught = false;

            try {
                var validator = new InlineValidator <Person> {
                    v => v.RuleFor(x => x.Surname).NotNull().WithSeverity(Severity.Warning)
                };
                validator.TestValidate(new Person())
                .ShouldHaveValidationErrorFor(x => x.Surname)
                .WithSeverity(Severity.Error);
            }
            catch (ValidationTestException e) {
                exceptionCaught = true;

                e.Message.ShouldEqual($"Expected a severity of '{nameof(Severity.Error)}'. Actual severity was '{nameof(Severity.Warning)}'");
            }

            exceptionCaught.ShouldBeTrue();
        }
Beispiel #25
0
        public void Matches_model_level_rule()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x).Must(x => false);
            validator.RuleFor(x => x.Surname).Must(x => false);

            var result = validator.TestValidate(new Person());

            bool thrown = false;

            try {
                result.ShouldHaveValidationErrorFor(x => x);
                result.ShouldHaveValidationErrorFor("");
            }
            catch (ValidationTestException) {
                thrown = true;
            }

            thrown.ShouldBeFalse();
        }
Beispiel #26
0
        public void Unexpected_message_check(string withoutErrMsg, string[] errMessages)
        {
            bool exceptionCaught = false;

            try {
                var validator = new InlineValidator <Person>();
                foreach (var msg in errMessages)
                {
                    validator.Add(v => v.RuleFor(x => x.Surname).NotNull().WithMessage(msg));
                }
                validator.TestValidate(new Person {
                }).Result.Errors.WithoutErrorMessage(withoutErrMsg);
            }
            catch (ValidationTestException e) {
                exceptionCaught = true;

                e.Message.ShouldEqual($"Found an unexpected error message of '{withoutErrMsg}'");
            }

            exceptionCaught.ShouldEqual(errMessages.Contains(withoutErrMsg));
        }
        public void Unexpected_state_check()
        {
            bool exceptionCaught = false;

            try {
                var validator = new InlineValidator <Person> {
                    v => v.RuleFor(x => x.Surname).NotNull().WithState(x => "bar"),
                    v => v.RuleFor(x => x.Surname).NotNull().WithState(x => "foo"),
                };
                validator.TestValidate(new Person())
                .ShouldHaveValidationErrorFor(x => x.Surname)
                .WithoutCustomState("bar");
            }
            catch (ValidationTestException e) {
                exceptionCaught = true;

                e.Message.ShouldEqual("Found an unexpected custom state of 'bar'");
            }

            exceptionCaught.ShouldBeTrue();
        }
        public void Expected_message_argument_check()
        {
            bool exceptionCaught = false;

            try {
                var validator = new InlineValidator <Person> {
                    v => v.RuleFor(x => x.Surname)
                    .Must((x, y, context) => {
                        context.MessageFormatter.AppendArgument("Foo", "bar");
                        return(false);
                    })
                    .WithMessage("{Foo}")
                };
                validator.TestValidate(new Person())
                .ShouldHaveValidationErrorFor(x => x.Surname)
                .WithMessageArgument("Foo", "foo");
            }
            catch (ValidationTestException e) {
                exceptionCaught = true;
                e.Message.ShouldEqual("Expected message argument 'Foo' with value 'foo'. Actual value was 'bar'");
            }

            exceptionCaught.ShouldBeTrue();
        }