I am NSubstituting for an Interface and Having trouble with the usage of passed Parameters (i,j):
The first two lines 25 and 26 are fine but I am having trouble with 27,,29
I need to use the arguments (i,j) they passed to me.
How might i correctly use NSubstitute?
Here is the Interface I am NSubstituting for:
public interface IGamePieceFactory
{
IGamePieceViewModel Create(int i, int j);
IGamePieceViewModel Create();
}
>Solution :
To use the values passed into the call for building your return value, you need to use an overloaded version of Returns that takes the arguments as parameters:
mockGameFactory
.Create(Arg.Any<int>(), Arg.Any<int>())
.Returns(args => new GamePieceViewModel
{
Point = new Point((int)args[0], (int)args[1])
}
This is detailed here in the documentation in advanced usages:
I’d recommend paying special attention to the comment there, however:
Although this is normally a bad practice, there are some situations in which it is useful.
I’d agree that this is usually a bad practice and that you should look into how you are testing if you need to rely on this feature too much.
