Question

I have the following models (all code simplified)

public class BiddingTierSetViewModel
{
   public List<BiddingTierViewModel> BiddingTiers { get; set; }
}

public class BiddingTierViewModel
    {
        public decimal StartValue { get; set; }
        public decimal? EndValue { get; set; }
        public decimal StepAmount { get; set; }
    }

I have a validator for the set

public class BiddingTierSetViewModelValidator : AbstractValidator<BiddingTierSetViewModel>
{
public BiddingTierSetViewModelValidator(IValidator<BiddingTierViewModel> biddingTierViewModelValidator)
{
    RuleFor(x => x.BiddingTiers).SetCollectionValidator(biddingTierViewModelValidator);
}
}

And for each element in the collection

public class BiddingTierViewModelValidator : AbstractValidator<BiddingTierViewModel>
    {
        public BiddingTierViewModelValidator()
        {
            RuleFor(x => x.StartValue).GreaterThanOrEqualTo(0M);
        }
    }

Now, in my test I set the startvalue to be less than zero

var validator = new BiddingTierViewModelValidator();
            var biddingTiers =
                new List<BiddingTierViewModel>() 
                { 
                    new BiddingTierViewModel() 
                    {
                        StartValue = -1,
                        EndValue = 100
                    }
                };
  biddingTierSetViewModelValidator = new BiddingTierSetViewModelValidator(
                validator);

            biddingTierSetViewModelValidator.ShouldHaveValidationErrorFor(b => b.BiddingTiers,
                biddingTiers);     

When I run my test I get the error

FluentValidation.TestHelper.ValidationTestException : Expected a validation error for the property BiddingTiers

Why is my validator not throwing an exception?

Was it helpful?

Solution

Well, I think you can't get errors when you use SetCollectionValidator and you don't validate the "parent" of the collection (here, you try to validate the collection, but not the parent (BiddingTierSetViewModel)

See this post

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top