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

mock.setup It.IsAny or It.Is instead new object()

How come it´s working with below example?

mock.Setup(p => p.GetUCByOrgNumberAsync(It.IsAny<UCPartyModel>))).ReturnsAsync(new PartyUCBusinessDTO() { Orgnr = "xxxxxxxx" });

But not with:

mock.Setup(p => p.GetUCByOrgNumberAsync(new UCPartyModel())).ReturnsAsync(new PartyUCBusinessDTO() { Orgnr = "xxxxxxxx" });

Data is null in below if I don’t use It.IsAny or It.Is

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

    private IActionResult CreateResult<T>(T data)
    {
        return CreateResult<T>(200, data);
    }

>Solution :

new UCPartyModel() is creating a new instance of your model for setup, so when the mock will be setup, the the object passed vs what is defined in Setup is not same, that’s why it is not working. So either you can go with It.IsAny<UCPartyModel> or below approach.

var model = new UCPartyModel();
mock.Setup(p => p.GetUCByOrgNumberAsync(model).ReturnsAsync(new PartyUCBusinessDTO() { Orgnr = "xxxxxxxx" });
 // Your code of invocation.
someBusinessObj.Run(model);

With above you are passing the same instance of object so with that actual Setup method will be invoked.

More details about It.IsAny<TValue>

It matches any value of the given TValue type. You can read about it here

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