简体   繁体   English

mock.setup It.IsAny 或 It.Is 代替 new object()

[英]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如果我不使用 It.IsAny 或 It.Is,下面的数据为空

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

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. new UCPartyModel()正在为设置创建模型的新实例,因此当设置模拟时,传递的对象与设置中定义的对象不同,这就是它不起作用的原因。 So either you can go with It.IsAny<UCPartyModel> or below approach.因此,您可以使用It.IsAny<UCPartyModel>或以下方法。

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.在上面,您传递了相同的对象实例,因此将调用实际的 Setup 方法。

More details about It.IsAny<TValue>有关It.IsAny<TValue>的更多详细信息

It matches any value of the given TValue type.它匹配给定 TValue 类型的任何值。 You can read about it here你可以在这里阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM