ShouldHaveValidationErrorFor and ShouldNotHaveValidationErrorFor needs identity overload
See original GitHub issueThis 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.
Issue Analytics
- State:
- Created 5 years ago
- Comments:32 (14 by maintainers)
Top Results From Across the Web
9.0 Upgrade Guide — FluentValidation documentation
... to ShouldHaveValidationErrorFor and ShouldNotHaveValidationErrorFor , eg: ... Validate(object model) overload has been removed to improve type safety.
Read more >FluentValidation test when fields match
But there is no overload for ShouldHaveValidationErrorFor that takes 1 argument. Short of declaring a local variable and setting the value to ...
Read more >How to Use FluentValidation in ASP.NET Core
Learn how to make use of FluentValidation in an ASP.NET Core application, to validate user input and make our applications easier to maintain...
Read more >FluentValidation in ASP.NET WebAPI | by Adam Connelly
ShouldHaveValidationErrorFor () and ShouldNotHaveValidationErrorFor() are extension methods provided by FluentValidation.net that make it ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.