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

New Post: FluentValidation: RuleSet unit testing

$
0
0
I created this class below to help me validate Rule Sets. I didn't test it a lot, but it works for me.
public static class ValidationTestExtension
{
    public static void ShouldHaveValidationErrorFor<T>
        (this IValidator<T> validator, string propertyName, T objectToTest,
        string ruleSet) where T : class
    {
        var results = validator.Validate(objectToTest, ruleSet: ruleSet);
        results.Errors.Any(e => e.PropertyName == propertyName).Should().Be(true);
    }

    public static void ShouldNotHaveValidationErrorFor<T>
        (this IValidator<T> validator, string propertyName, T objectToTest,
        string ruleSet) where T : class
    {
        var results = validator.Validate(objectToTest, ruleSet: ruleSet);
        results.Errors.Any(e => e.PropertyName == propertyName).Should().Be(false);
    }
}
Example:
var user = new User { Email = "" };
_validator.ShouldHaveValidationErrorFor("Email", user, "SignUpRuleSet");

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>