I'm using the FluentValidation library in an ASP.NET MVC project and, from a UI perspective, it's working as expected. Rule violations display the correct errors.
I have a parent class that has a validator and a collection property where that type has a validator. It's conceptually the same as described in the documentation.
I have a validator for a parent class...
public class MyFormValidator : AbstractValidator<MyFormViewModel>
...and I have a collection in MyFormViewModel
...
public IList<ChildRow> ChildRowsAdded { get; set; }
...and I create a validator for the collection of that child class...
public class ChildRowValidator : AbstractValidator<ChildRow>
...and I use that child validator in the parent validator...
RuleFor(m => m.ChildRowsAdded).SetCollectionValidator(new ChildRowValidator());
While writing some unit tests, I noticed that ShouldHaveValidationErrorFor
is not confirming the errors exist.
_validator.ShouldHaveValidationErrorFor(x => x.ChildRowsAdded, model);
That line in my test does not seem to see the errors. The test fails and the message says
FluentValidation.TestHelper.ValidationTestException : Expected a validation error for property AllergyRowsAdded.
If I manually .Validate()
and look at the results, I see the error.
Has anyone run into this before? Is there an additional step I need to take to use ShouldHaveValidationErrorFor
in this situation?
validator.ShouldHaveValidationErrorFor(m => m.Childs[0].Name, model);
but it doesn't work either. When I looked into FluentValidation source codes it seems that it is not possible to useShouldHaveValidationErrorFor
assert for this type of test. – Drummer