Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NSubstitute .Returns() and Arg,Any<int> How to use the passed parametrers for Subsituted interface

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?

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading