mercredi 15 juin 2016

Unit tests with FluentValidation. How to mock ValidationResult

I'm using TDD approach with xUnit 2, NSubstitute, AutoFixture, FluentAssertions for my unit tests. I want test my service method which using FluentValidation.

Simple example: Validator:

RuleSet("Nulls", () =>
        {
            RuleFor(viewModel => viewModel).NotNull();
        });

My service(under test):

if(Validate(viewModel, "Nulls"))

 private bool Validate(AddMerchantViewModel viewModel, string option)
    {
        var result = _merchantValidator.Validate(viewModel, ruleSet: options);
        return result.IsValid;
    }

And my unit test: I don't know how to mock the merchantValidator.Validate result.

  [Theory, AutoNSubstituteData]
    public void Add_ViewModelAsNull_ShouldThrowArgumentNullException(
        AbstractValidator<AddMerchantViewModel> merchValidator,
        MerchantsService service)
    {
        // Arrange


        here I dont know how to mock result of Validate. It is alaways null.
  merchantValidator.Validate(Arg.Any<AddMerchantViewModel>(), ruleSet: Arg.Any<string>()).Return(new ValidationResult()); 



        // Act
        Action action = () => service.Add(null);

        // Assert
        action.ShouldThrow<ArgumentNullException>();
    } 

Aucun commentaire:

Enregistrer un commentaire