Beispiel #1
0
        public static void ValidateProperty_throws_ValidationException_if_Required_attribute_test_fails()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyWithRequiredAttribute";
            var exception = Assert.Throws <ValidationException>(
                () => Validator.ValidateProperty(null, validationContext));

            AssertEx.IsType <RequiredAttribute>(exception.ValidationAttribute);
            // cannot check error message - not defined on ret builds
            Assert.Null(exception.Value);
        }
Beispiel #2
0
        public static void ValidateProperty_throws_ValidationException_if_errors()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyToBeTested";
            var exception = Assert.Throws <ValidationException>(
                () => Validator.ValidateProperty("Invalid Value", validationContext));

            AssertEx.IsType <ValidValueStringPropertyAttribute>(exception.ValidationAttribute);
            Assert.Equal("ValidValueStringPropertyAttribute.IsValid failed for value Invalid Value", exception.ValidationResult.ErrorMessage);
            Assert.Equal("Invalid Value", exception.Value);
        }
Beispiel #3
0
        public static void TestValidateValueThrowsIfNoRequiredAttribute()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyWithRequiredAttribute";
            var attributesToValidate = new ValidationAttribute[] { new ValidValueStringPropertyAttribute() };
            var exception            = Assert.Throws <ValidationException>(
                () => Validator.ValidateValue("Invalid Value", validationContext, attributesToValidate));

            AssertEx.IsType <ValidValueStringPropertyAttribute>(exception.ValidationAttribute);
            Assert.Equal("ValidValueStringPropertyAttribute.IsValid failed for value Invalid Value", exception.ValidationResult.ErrorMessage);
            Assert.Equal("Invalid Value", exception.Value);
        }
Beispiel #4
0
        public static void TestValidateValueThrowsIfNullRequiredAttribute()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyWithRequiredAttribute";
            var attributesToValidate = new ValidationAttribute[] { new RequiredAttribute(), new ValidValueStringPropertyAttribute() };
            var exception            = Assert.Throws <ValidationException>(
                () => Validator.ValidateValue(null, validationContext, attributesToValidate));

            AssertEx.IsType <RequiredAttribute>(exception.ValidationAttribute);
            // cannot check error message - not defined on ret builds
            Assert.Null(exception.Value);
        }
Beispiel #5
0
        public static void TestValidateObjectThrowsIfRequiredTestFails()
        {
            var objectToBeValidated = new ToBeValidated()
            {
                PropertyWithRequiredAttribute = null
            };
            var validationContext = new ValidationContext(objectToBeValidated);
            var exception         = Assert.Throws <ValidationException>(
                () => Validator.ValidateObject(objectToBeValidated, validationContext, true));

            AssertEx.IsType <RequiredAttribute>(exception.ValidationAttribute);
            // cannot check error message - not defined on ret builds
            Assert.Null(exception.Value);
        }
Beispiel #6
0
        public static void ValidateObject_throws_ValidationException_if_errors()
        {
            var objectToBeValidated = new ToBeValidated()
            {
                PropertyToBeTested            = "Invalid Value",
                PropertyWithRequiredAttribute = "Valid Value"
            };
            var validationContext = new ValidationContext(objectToBeValidated);
            var exception         = Assert.Throws <ValidationException>(
                () => Validator.ValidateObject(objectToBeValidated, validationContext, true));

            AssertEx.IsType <ValidValueStringPropertyAttribute>(exception.ValidationAttribute);
            Assert.Equal("ValidValueStringPropertyAttribute.IsValid failed for value Invalid Value", exception.ValidationResult.ErrorMessage);
            Assert.Equal("Invalid Value", exception.Value);
        }
Beispiel #7
0
        public static void ValidateObject_throws_ValidationException_if_all_properties_are_valid_but_class_is_invalid()
        {
            var objectToBeValidated = new InvalidToBeValidated()
            {
                PropertyWithRequiredAttribute = "Valid Value"
            };
            var validationContext = new ValidationContext(objectToBeValidated);
            var exception         = Assert.Throws <ValidationException>(
                () => Validator.ValidateObject(objectToBeValidated, validationContext, true));

            AssertEx.IsType <ValidClassAttribute>(exception.ValidationAttribute);
            Assert.Equal(
                "ValidClassAttribute.IsValid failed for class of type " + typeof(InvalidToBeValidated).FullName,
                exception.ValidationResult.ErrorMessage);
            Assert.Equal(objectToBeValidated, exception.Value);
        }