I wrote some custom validation for some Entity Framework objects using the IValidatableObject method, and I added some DataAnnotations to the objects for validation.
I wanted to verify that validation matches the required validation (ensuring that the user validation is functional and that any changes made keep these data annotations, etc.), but I cannot determine how to perform the validation in unit test without calling SaveChanges (which I I donβt want to do it, as if there is a problem, and the verification does not work, it will write to the data source)
I wanted to do something like this:
[TestMethod] public void InvalidStartDate_StartDateAfterEndDate() { var header = new Header() { StartDate = DateTime.Now.AddDays(15), EndDate = DateTime.Now.AddDays(-15) }; var actual = header.IsValid(); var expected = false; Assert.AreEqual(expected, actual); }
Or something like
[TestMethod] public void InvalidStartDate_StartDateAfterEndDate() { var header = new Header() { StartDate = DateTime.Now.AddDays(15), EndDate = DateTime.Now.AddDays(-15) }; var actual = header.GetValidationErrors().Count; var expected = 0; Assert.AreEqual(expected, actual); }
But it doesn't seem to be able to find a way to start validation without invoking save changes, is there a way to do this?
source share