예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
0
 public static void ValidateValueThrowsIf_ValidationContext_is_null()
 {
     Assert.Throws <ArgumentNullException>(
         () => Validator.ValidateValue(new object(),
                                       validationContext: null, validationAttributes: Enumerable.Empty <ValidationAttribute>()));
 }