C# 使用 Moq,如何将输入参数设置为具有预期属性值的对象的方法调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15879260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:16:00  来源:igfitidea点击:

Using Moq, how do I set up a method call with an input parameter as an object with expected property values?

c#unit-testingtddmoq

提问by The Light

 var storageManager = new Mock<IStorageManager>(); 
 storageManager.Setup(e => e.Add(It.IsAny<UserMetaData>()));

The Add() method expects a UserMetaData object which has a FirstName property.

Add() 方法需要一个具有 FirstName 属性的 UserMetaData 对象。

I'd like to make sure that an object of type UserMetaData with the FirstName of "FirstName1" has been passed.

我想确保已传递 FirstName 为“FirstName1”的 UserMetaData 类型的对象。

采纳答案by sloth

You can use Verify.

您可以使用Verify.

Examples:

例子:

Verify that Addwas never called with an UserMetaDatawith FirstName!= "FirstName1":

验证Add从未用UserMetaDatawith FirstName!=调用过"FirstName1"

storageManager.Verify(e => e.Add(It.Is<UserMetaData>(d => d.FirstName!="FirstName1")), Times.Never());

Verify that Addwas called at least once with an UserMetaDatawith FirstName== "FirstName1":

Add使用UserMetaDatawith FirstName==验证至少调用了一次"FirstName1"

storageManager.Verify(e => e.Add(It.Is<UserMetaData>(d => d.FirstName=="FirstName1")), Times.AtLeastOnce());

Verify that Addwas called exactly once with FirstName== "Firstname1"and LastName== "LastName2":

验证Add使用FirstName=="Firstname1"LastName==调用了一次"LastName2"

storageManager.Setup(e => e.Add(It.Is<UserMetaData>(data => data.FirstName == "FirstName1"
                                                         && data.LastName  == "LastName2")));

...

storageManager.VerifyAll();

回答by MatthiasG

You can use the It.Is method:

您可以使用 It.Is 方法:

storageManager.Setup(e => e.Add(It.Is<UserMetaData>(data => data.FirstName == "FirstName1")));

回答by Andy Nichols

Dominic Kexel's method is good and will work. You can also use callback though which is useful if you need to do any checking of the output that is more complicated.

Dominic Kexel 的方法很好,会奏效。您也可以使用回调,如果您需要对更复杂的输出进行任何检查,这很有用。

 UserMetaData parameter = null;
 var storageManager = new Mock<IStorageManager>(); 
 storageManager
    .Setup(e => e.Add(It.IsAny<UserMetaData>()))
    .Callback((UserMetaData metaData) => parameter = metaData);

 Assert.That(parameter.FirstName, Is.EqualTo("FirstName1")); //If using fluent NUnit

The advantage of this is that, if required, you can do many more checks on the parameter rather than just checking that it is "FirstName1".

这样做的好处是,如果需要,您可以对参数进行更多检查,而不仅仅是检查它是否为“FirstName1”。

The disadvantage is that if Add is called multiple times then only the parameter passed in the last call will be checked (although you can additionally Verify that it was called once).

缺点是如果 Add 被多次调用,那么只会检查最后一次调用中传递的参数(尽管您可以另外验证它是否被调用过一次)。

Dominic's answer is better than mine for your precise situation but I wanted to point out Callback for other similar situations.

对于您的具体情况,多米尼克的回答比我的要好,但我想指出其他类似情况的回调。