コード例 #1
0
        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();
        }
コード例 #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();
        }
コード例 #3
0
        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();
        }
コード例 #4
0
        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");
        }
コード例 #5
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());

            result.ShouldHaveValidationErrorFor(x => x);
            result.ShouldHaveValidationErrorFor("");
        }
コード例 #6
0
        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();
        }
コード例 #7
0
        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();
        }
コード例 #8
0
        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'");
        }
コード例 #9
0
        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();
        }
コード例 #10
0
        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'");
        }
コード例 #11
0
        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)}'");
        }
コード例 #12
0
        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");
        }
コード例 #13
0
        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");
        }
コード例 #14
0
        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);
        }
コード例 #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("");
            });
        }
コード例 #16
0
        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}'");
        }
コード例 #17
0
        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.'");
        }
コード例 #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");
        }
コード例 #19
0
        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);
        }
コード例 #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");
        }
コード例 #21
0
        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'");
        }
コード例 #22
0
        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");
        }
コード例 #23
0
        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();
        }
コード例 #24
0
        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();
        }
コード例 #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();
        }
コード例 #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));
        }
コード例 #27
0
        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();
        }
コード例 #28
0
        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();
        }