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