Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Using ShouldHaveValidationErrorFor with SetCollectionValidator

$
0
0
Apologies for cross-posting, but I wanted to see if anyone had additional information on this issue.

http://stackoverflow.com/questions/28354857/fluentvalidation-shouldhavevalidationerrorfor-with-setcollectionvalidator

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?

Viewing all articles
Browse latest Browse all 1917

Trending Articles