コード例 #1
0
        public void Only_root_validator_throws()
        {
            var validator        = new InlineValidator <Person>();
            var addressValidator = new InlineValidator <Address>();

            validator.RuleFor(x => x.Address).SetValidator(addressValidator);
            validator.RuleFor(x => x.Forename).NotNull();
            addressValidator.RuleFor(x => x.Line1).NotNull();

            bool thrown = false;

            // Child validator shouldn't throw the exception, only the root validator should.
            // If the child validator threw the exception, then there would only be 1 failure in the validation result.
            // But if the root is throwing, then there should be 2 (as it collected its own failure and the child failure).
            try {
                validator.ValidateAndThrow(new Person()
                {
                    Address = new Address()
                });
            }
            catch (ValidationException e) {
                e.Errors.Count().ShouldEqual(2);
                thrown = true;
            }

            thrown.ShouldBeTrue();
        }