'How reuse Custom Validator in other tests

I had a class and I wrote validation rules for it, but then I understand that the exact same validation would be for all classes with ID. Then I decided to write an extension to use it everywhere. But I ran into the problem of writing tests. I don't understand how to reuse the test for other classes

i have next validator and model:

public class AddValidator : AbstractValidator<AddCommand>
{      
    public AddValidator()
    {
        this.RuleFor(i => i.Id)
            .Cascade(CascadeMode.Stop)
            .CustomIdValidator();
    }
}
public class AddCommand
{
    public string Id { get; set; }
}

extension witch i want reuse in other validators:

public static class IdValidatorExtension
{
        public static IRuleBuilder<T, string> CustomIdValidator<T>(
            this IRuleBuilder<T, string> ruleBuilder)
        {
            return ruleBuilder.NotEmpty().NotNull();
        }
}

before i write this extensions i create tests(nUnit)

[TestCase("")]
[TestCase(null)]
public void WhenIdNullOrEmptyValue_ShouldThrowError(string id)
{
     this.target.TestValidate(new SomeCommand { Id = id })
         .ShouldHaveValidationErrorFor(i => i.Id);
}

model:

public class SomeCommand 
{
    public string Id { get; set; }
}

and now i don't know how to reuse this test in all my model where i have property id



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source