In connection with this question
I created my own DateValidationAttibute to make sure the string is in a valid date format (e.g. MM / DD / YYYY)
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class DateValidationAttribute : DataTypeAttribute
{
public DateValidationAttribute() : base(DataType.Date){}
public override bool IsValid(object value)
{
}
}
I am trying to verify this attribute with this code
[Test]
public void Test()
{
var invalidObject = new TestValidation {DateField = "bah"};
var validationContext = new ValidationContext(invalidObject, null, null);
var validationResults = new System.Collections.Generic.List<ValidationResult>();
bool result = Validator.TryValidateObject(invalidObject, validationContext, validationResults);
Assert.IsFalse(result);
Assert.AreEqual(1, validationResults.Count);
}
private class TestValidation
{
[DateValidation(ErrorMessage = "Invalid Date!")]
public string DateField { get; set; }
}
Unfortunately this does not work. I set a breakpoint in the DateValidationAttribute constructor and the IsValid method. It definitely hits the constructor, but never removes the IsValid method. Any ideas?
source
share