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);
        }
        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);
        }
        public void KeyTest()
        {
            var c = new TestClass() { Id = 1000, Email = "*****@*****.**" };
            var kg = new IndexKeyGenerator(new IdentityKeyGenerator());
            var keys = kg.Generate(c);

            var k1 = keys.First();
            Assert.AreEqual("id:1000:email", k1.PropertyName);
            Assert.AreEqual(c.Email.ToLowerInvariant(), k1.PropertyValue);

            var k2 = keys.Last();
            Assert.AreEqual("email:[email protected]:id", k2.PropertyName);
            Assert.AreEqual(c.Id.ToString(), k2.PropertyValue);
        }
        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.IsInstanceOfType(typeof (RequiredAttribute),
                                    result.Errors.Where(e => e.PropertyName == "Foo").Single().Validator);
            Assert.IsInstanceOfType(typeof (StringLengthAttribute),
                                    result.Errors.Where(e => e.PropertyName == "Bar").Single().Validator);
        }
        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);
        }
示例#6
0
        public static void Constructor_creates_new_instance_for_one_arg_constructor()
        {
            var testDataAnnotationsDerived = new TestClass();

            AssertEx.DoesNotThrow(() => new ValidationContext(testDataAnnotationsDerived));
        }