Example #1
0
        public ValidationResult CombineWith(ValidationResult other)
        {
            Requires.NotNull("other", other);

            //Just concatenate the errors collection
            return new ValidationResult(_Errors.Concat(other.Errors));
        }
Example #2
0
        public void ValidateObject_Returns_Failed_Result_If_Any_Attribute_Does_Not_Validate()
        {
            // Arrange
            TestClass cls = new TestClass {
                Foo = new object(), Bar = "BarBaz"
            };
            DataAnnotationsObjectValidator validator = new DataAnnotationsObjectValidator();

            // Act
            ValidationResult result = validator.ValidateObject(cls);

            // Assert
            Assert.IsFalse(result.IsValid);
        }
Example #3
0
        public void ValidateObject_Returns_Successful_Result_If_All_Attributes_On_All_Properties_Validate()
        {
            // Arrange
            TestClass cls = new TestClass {
                Foo = new object(), Bar = "Baz"
            };
            DataAnnotationsObjectValidator validator = new DataAnnotationsObjectValidator();

            // Act
            ValidationResult result = validator.ValidateObject(cls);

            // Assert
            Assert.IsTrue(result.IsValid);
        }
Example #4
0
        public void ValidateObject_Collects_Error_Messages_From_Validation_Attributes()
        {
            // Arrange
            TestClass cls = new TestClass {
                Foo = null, Bar = "BarBaz"
            };
            DataAnnotationsObjectValidator validator = new DataAnnotationsObjectValidator();

            // Act
            ValidationResult result = validator.ValidateObject(cls);

            // Assert
            Assert.IsFalse(result.IsValid);
            Assert.AreEqual("Dude, you forgot to enter a Foo", result.Errors.Where(e => e.PropertyName == "Foo").Single().ErrorMessage);
            Assert.AreEqual("Yo, you have to specify 5 characters for Bar", result.Errors.Where(e => e.PropertyName == "Bar").Single().ErrorMessage);
        }
Example #5
0
        public void ValidateObject_Collects_ValidationAttribute_Objects_From_Failed_Validation()
        {
            // Arrange
            TestClass cls = new TestClass {
                Foo = null, Bar = "BarBaz"
            };
            DataAnnotationsObjectValidator validator = new DataAnnotationsObjectValidator();

            // Act
            ValidationResult result = validator.ValidateObject(cls);

            // Assert
            Assert.IsFalse(result.IsValid);
            Assert.IsInstanceOf <RequiredAttribute>(result.Errors.Single(e => e.PropertyName == "Foo").Validator);
            Assert.IsInstanceOf <StringLengthAttribute>(result.Errors.Single(e => e.PropertyName == "Bar").Validator);
        }