2
votes

I am setting up Moq like:

mockCommand.Setup(foo => foo.Post<Foo, Response>(It.IsAny<Foo>()))
    .Returns((Foo m) =>
    {
        if (m . . . .)
        {
            return new <Response>()
            {
                <...>
            };
        }
        else
        {
            return new <Response>()
            {
                <...>
            };
        }
    });

But I get an exception:

System.ArgumentException : Invalid callback. Setup on method with 2 parameter(s) cannot invoke callback with different number of parameters (1).

The generic method is defined as:

TResponse Post<TRequest, TResponse>(TRequest request, params string[] query)

The only thing that I can think of is that somehow because I am conditionally returning response it is somehow confusing Moq so it throws the exception. Ideas?

1

1 Answers

2
votes

The exception is pointing out that parameters were omitted from the setup and the Returns callback delegate.

Include all the parameters for the member being mocked

mockCommand
    .Setup(foo => foo.Post<Foo, Response>(It.IsAny<Foo>(), It.IsAny<string[]>()))
    .Returns((Foo m, string[] q) => {
        //...omitted for brevity
    });