question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ShouldHaveValidationErrorFor and ShouldNotHaveValidationErrorFor needs identity overload

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:32 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Aug 27, 2020

Apparently I added it in 8.5, which was in September last year! 🤷‍♂️

1reaction
JeremySkinnercommented, May 15, 2019

@jzabroski delete the (generated) .build directory and the error will go away.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found