Example #1
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.ShouldHaveValidationErrorFor(x => x.Surname, null as string).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();
        }
Example #2
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.ShouldHaveValidationErrorFor(x => x.Surname, null as string).WithErrorCode("foo");
            }
            catch (ValidationTestException e) {
                exceptionCaught = true;

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

            exceptionCaught.ShouldBeTrue();
        }
Example #3
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.ShouldHaveValidationErrorFor(x => x.Surname, null as string).WithoutCustomState("bar");
            }
            catch (ValidationTestException e) {
                exceptionCaught = true;

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

            exceptionCaught.ShouldBeTrue();
        }
Example #4
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.ShouldHaveValidationErrorFor(x => x.Surname, null as string).WithMessageArgument("Foo", "foo");
            }
            catch (ValidationTestException e) {
                exceptionCaught = true;
                e.Message.ShouldEqual("Expected message argument 'Foo' with value 'foo'. Actual value was 'bar'");
            }

            exceptionCaught.ShouldBeTrue();
        }