This is an up-to-date version of this question, as the solution no longer applies.
I've implemented fluent validation and am now building tests. The developers of FluentValidations strongly suggest that we don't mock/fake the validator, and instead use their provided InlineValidator.
Unfortunately they don't explain how the result can be configured/specified.
Someone asked this question back in 2016 and the answer was to use .RuleFor(...). However, this method no longer exists on InlineValidator.
So what's the correct way of configuring the result of a call to .Validate or .ValidateAsync when testing?
Here's what I've tried so far: I implemented my own class, derived from InlineValidator, and implemented ValidateAsync...
public class MyInlineValidator<T>(IEnumerable<ValidationFailure> validationFailures) : InlineValidator<T>
{
public Task<ValidationResult> ValidateAsync<T>(T instance,
Action<ValidationStrategy<T>> options,
CancellationToken cancellation = default)
{
return Task.FromResult(new ValidationResult(validationFailures));
}
}
But I can't get this to work when passing an instance of this into my class-under-test: the original ValidateAsync is an extension method, and the runtime keeps selecting that implementation over my custom implementation.