Injecting validators in FluentValidation

1

This is my setup:

public class BusinessUnitIdValidator : AbstractValidator<string>
{
    public BusinessUnitIdValidator(IConfigSettings configSettings)
    {
        LovFile buidLovFile = configSettings.BuidLovFile;
        When(x => buidLovFile.IsValidationRequired.Equals("true", StringComparison.OrdinalIgnoreCase),
            () =>
            {
                RuleFor(x => x).Must((buid) => IsValidBusinessUnitId(buidLovFile, buid)).WithMessage(ValidationResultConstants.INVALID_BUSINESS_UNIT_ID);
            });
    }
}

public CreateCustomerRequestValidator(IValidator<string> _businessUnitIdValidator)
{
    RuleFor(x => x).NotNull();
    RuleFor(x => x.BusinessUnitId).NotNull().WithMessage(ValidationResultConstants.BUSINESS_UNIT_ID_SHOULD_NOT_BE_NULL);
    RuleFor(x => x.BusinessUnitId).SetValidator(_businessUnitIdValidator);
}

This is another validator that I like to unit test. I'm mocking the behavior of BusinessUnitIdValidator so it should just pass without any issues.

List<ValidationFailure> validationFailures = new List<ValidationFailure>();
ValidationResult validationResultMock = new ValidationResult(validationFailures);
Mock<IValidator<string>> _businessUnitIdValidatorMock = new Mock<IValidator<string>>();
_businessUnitIdValidatorMock.Setup(b => b.Validate(It.IsAny<string>())).Returns(validationResultMock);
validator = new CreateCustomerRequestValidator(_businessUnitIdValidatorMock.Object);

CreateCustomerRequest createCustomerRequest = GetCreateCustomerRequest();

//Act
var validationResult = validator.Validate(createCustomerRequest);

I'm getting an null reference exception:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=FluentValidation
  StackTrace:
   at FluentValidation.Validators.ChildValidatorAdaptor.Validate(PropertyValidatorContext context) in /home/jskinner/code/FluentValidation/src/FluentValidation/Validators/ChildValidatorAdaptor.cs:line 56
   at FluentValidation.Internal.PropertyRule.<Validate>d__65.MoveNext() in /home/jskinner/code/FluentValidation/src/FluentValidation/Internal/PropertyRule.cs:line 282
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at FluentValidation.AbstractValidator`1.Validate(ValidationContext`1 context) in /home/jskinner/code/FluentValidation/src/FluentValidation/AbstractValidator.cs:line 115
   at Customer.UnitTests.Libraries.CustomerModelValidators.CreateCustomerRequestValidatorTests.ShouldPass() 

My integration tests are working fine though.

Thanks, Arun

c#
fluentvalidation
asked on Stack Overflow Sep 10, 2019 by Arun

1 Answer

2

I found the answer to this: I was mocking the incorrect version of the Validate method.

_businessUnitIdValidatorMock.Setup(b => b.Validate(It.IsAny<ValidationContext>())).Returns(validationResultMock);

Thanks, Arun

answered on Stack Overflow Sep 10, 2019 by Arun

User contributions licensed under CC BY-SA 3.0