Buda Gavril
Buda Gavril

Reputation: 21657

mock function with variable number of aguments using NSubstitute

I have an interface with the following method:

bool ProcessActions(int actionTypeId, out List<int> ints, params object[] actionParameters);

Now how can mock this method to return a value using NSubstitute? Here is what I've tried:

    this.actionOperationsMock.ProcessActions(Arg.Any<int>(), out List<int> _, Arg.Any<int>(), Arg.Any<int>(), Arg.Any<int>(), Arg.Any<DateTime>(), Arg.Any<DateTime>(), Arg.Any<string>()).Returns(
        x =>
            {
                x[1] = null;
                return true;
            });

I've tried to mock it only with the first two parameters, but in my tests, this method is returning false.

Upvotes: 0

Views: 399

Answers (1)

LukaszBalazy
LukaszBalazy

Reputation: 577

I'd advise to use ReturnsForAnyArgs since you are going to mock each one of parameters and params object[] don't have to be mocked in that case since params allow 0 arguments.

mock.ProcessActions(Arg.Any<int>(), out Arg.Any<List<int>>()).ReturnsForAnyArgs(
            x =>
            {
                x[1] = null;
                return true;
            });

        var res = mock.ProcessActions(1, out var list, 1, 1, 1 , DateTime.Now, DateTime.Now.AddHours(2), "");

Upvotes: 1

Related Questions