3

I am trying to use Moq to mock a reponse from a method which uses the params keyword with an int array

public interface IValidationHelper
{
    Task<bool> ValidateParents(params int?[] parents);
}

I am findning myself having to mock it in two ways to get it to properly mock. First, with a single param, and second, with two params. Is there a way to specify something like params in a It.IsAny in the Setup?

private void MockValidateParents(bool valid = true)
{
    _validationHelper.Setup(x => x.ValidateParents(
        It.IsAny<int>()
    )).ReturnsAsync(valid);

    _validationHelper.Setup(x => x.ValidateParents(
        It.IsAny<int>(),
        It.IsAny<int>()
    )).ReturnsAsync(valid);
}
blgrnboy
  • 4,877
  • 10
  • 43
  • 94

1 Answers1

4

I think you need to tell the mock that it is any array that it can take:

_validationHelper.Setup(x => x.ValidateParents(
    It.IsAny<int?[]>()
)).ReturnsAsync(valid);

Instead of telling it all the single inputs.

Kristian Barrett
  • 3,574
  • 2
  • 26
  • 40