FluentValidation.Validators.UnitTestExtension
FluentValidation.Validators.UnitTestExtension copied to clipboard
Rules of independent properties are expected in tested property
Hello,
in the code underneath I try to check if the validation rule GreaterThan(0) for the id of the Driver object in a Shift is present. Unfortunately the test always fails with the message: Expected collection to contain 1 item(s) because (number of rules for property), but found 3.
It seems like somehow the rules for Assistant.Id and Trainee.Id influence the ones for Driver.Id.
public class BugTests
{
private readonly DutyRosterValidator dutyRosterValidator =
new DutyRosterValidator();
[Test]
public void DriverId_ShouldHaveAllValidators()
{
dutyRosterValidator.ShouldHaveRules(x => x.Driver.Id,
BaseVerifiersSetComposer.Build()
.AddPropertyValidatorVerifier<GreaterThanValidator>(0)
.Create());
}
public class User
{
public int Id { get; set; }
}
public class Shift
{
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public User Driver { get; set; }
public User Assistant { get; set; }
public User Trainee { get; set; }
}
public class DutyRosterValidator : AbstractValidator<Shift>
{
public DutyRosterValidator()
{
RuleFor(x => x.Driver.Id)
.GreaterThan(0)
.When(x => x.Driver != null);
RuleFor(x => x.Assistant.Id)
.GreaterThan(0)
.When(x => x.Assistant != null);
RuleFor(x => x.Trainee.Id)
.GreaterThan(0)
.When(x => x.Trainee != null);
}
}
}