FluentValidation: ShouldHaveValidationErrorFor and ShouldNotHaveValidationErrorFor needs identity overload
This won’t compile in C#:
// Mock.Of refers to Moq
validator.ShouldNotHaveValidationErrorFor<Model, Model>(
m => m,
Mock.Of<Model>,
"Create,CreateOrUpdate");
generates compile error:
Severity Code Description Project File Line Suppression State
Error CS0121 The call is ambiguous between the following methods or properties: 'ValidationTestExtension.ShouldHaveValidationErrorFor<T, TValue>(IValidator<T>, Expression<Func<T, TValue>>, TValue, string)' and 'ValidationTestExtension.ShouldHaveValidationErrorFor<T, TValue>(IValidator<T>, Expression<Func<T, TValue>>, T, string)' Tests C:\source\ModelValidatorTests.cs 86 Active
I believe we need an overload to express the Identity function relationship:
public static class ValidationTestExtension
{
public static IEnumerable<ValidationFailure> ShouldHaveValidationErrorFor<T>(this IValidator<T> validator, Expression<Func<T, T>> expression, T value, string ruleSet)
where T : class, new()
{
T instance = Activator.CreateInstance<T>();
return validator.TestValidate<T>(instance, ruleSet).ShouldHaveError<T, T>();
}
}
This is useful when writing validations that involve multiple properties.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 32 (14 by maintainers)
Commits related to this issue
- Fixes #910 for FV 9.0.0 API ShouldHaveValidationErrorFor and ShouldNotHaveValidationErrorFor needs identity overload — committed to jzabroski/FluentValidation by jzabroski 4 years ago
Apparently I added it in 8.5, which was in September last year! 🤷♂️
@jzabroski delete the (generated) .build directory and the error will go away.
Currently awaiting @jzabroski (or someone else) to submit a pull request with appropriate tests. Once that’s done and reviewed it can go into the next release.