Example #1
0
        public void ValidClassIsValidWithCustomRules()
        {
            var simple = new SimpleClass {
                Number = 2, Text = "A value"
            };
            var complex = new ComplexClass {
                Id = Guid.NewGuid(), Child = simple
            };
            var validator = new ComplexClassValidator(new SimpleClassValidator()) as Core.Validators.IValidator <ComplexClass>;
            var result    = validator?.Validate(complex, ComplexClassValidator.RuleSetName);

            validator.Should().NotBeNull("because the base validator should impleent the base IValidator<T> contract");
            result.Should().NotBeNull("because a set should be returned even for valid objects");
            result.Should().HaveCount(0, "because a valid object should have no errors");
        }
Example #2
0
        public void CustomRuleIsNotRunWhenNotRequested()
        {
            var simple = new SimpleClass {
                Number = 999, Text = null
            };
            var complex = new ComplexClass {
                Id = Guid.Empty, Child = simple
            };
            var validator = new ComplexClassValidator(new SimpleClassValidator()) as Core.Validators.IValidator <ComplexClass>;
            var result    = validator?.Validate(complex);

            validator.Should().NotBeNull("because the base validator should impleent the base IValidator<T> contract");
            result.Should().NotBeNull("because a set should be returned even for valid objects");
            result.Should().HaveCount(1, "because child should not have been validated");
            result.Count(error => error.Code == ErrorCode.InvalidValue.ToString()).Should().Be(1, "because the id property was the only property with a bad value");
        }
Example #3
0
        public void InValidClassIsVDetectedWithCustomRules()
        {
            var simple = new SimpleClass {
                Number = 999, Text = null
            };
            var complex = new ComplexClass {
                Id = Guid.Empty, Child = simple
            };
            var validator = new ComplexClassValidator(new SimpleClassValidator()) as Core.Validators.IValidator;
            var result    = validator?.Validate(complex, ComplexClassValidator.RuleSetName);

            validator.Should().NotBeNull("because the base validator should impleent the base IValidator<T> contract");
            result.Should().NotBeNull("because a set should be returned even for valid objects");
            result.Should().HaveCount(3, "because the Id and both child properties were invalid");
            result.Count(error => error.Code == ErrorCode.InvalidValue.ToString()).Should().Be(1, "because the id property was the only property with a bad value");
            result.Count(error => error.Code == ErrorCode.NumberIsOutOfRange.ToString()).Should().Be(1, "because the number property was the only property with a range error");
            result.Count(error => error.Code == ErrorCode.ValueIsRequired.ToString()).Should().Be(1, "because the text property was the only property missing a value");
        }