为什么使用It.is<或It.IsAny<如果我只能定义一个变量? [英] Why use It.is<> or It.IsAny<> if I could just define a variable?

查看:191
本文介绍了为什么使用It.is<或It.IsAny<如果我只能定义一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看到这段代码时,我已经使用moq一段时间了.

Hi I've been using moq for a while when I see this code.

我必须在我的一个回购单中设置退货.

I have to setup a return in one of my repo.

 mockIRole.Setup(r => r.GetSomething(It.IsAny<Guid>(), It.IsAny<Guid>(), 
                  It.IsAny<Guid>())).Returns(ReturnSomething);

我有三个参数,我只是在网上的一篇文章或博客中看到了这些参数.

I have three parameters and I just saw these in one of articles or blog on the net.

It.Is<>It.IsAny<>对一个对象有什么用途?如果我可以使用Guid.NewGuid()或其他类型,那为什么要使用It.Is?

What is the use of It.Is<> or It.IsAny<> for an object? if I could use Guid.NewGuid() or other types then why use It.Is?

很抱歉,我不确定我的问题是对的还是我在测试中缺少一些知识. 但这两种方式似乎都没有错.

I'm sorry I'm not sure if my question is right or am I missing some knowledge in testing. But it seems like there is nothing wrong either way.

推荐答案

使用It.IsAny<>It.Is<>或变量均具有不同的用途.它们提供了越来越具体的方法来设置或验证方法时匹配参数.

Using It.IsAny<>, It.Is<>, or a variable all serve different purposes. They provide increasingly specific ways to match a parameter when setting up or verifying a method.

使用It.IsAny<>设置的方法将匹配您给该方法提供的 any 参数.因此,在您的示例中,以下调用将全部返回相同的内容(ReturnSomething):

The method set up with It.IsAny<> will match any parameter you give to the method. So, in your example, the following invocations would all return the same thing (ReturnSomething):

role.GetSomething(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid());

Guid sameGuid = Guid.NewGuid();
role.GetSomething(sameGuid, sameGuid, sameGuid);

role.GetSomething(Guid.Empty, Guid.NewGuid(), sameGuid);

所传递的Guid的实际值无关紧要.

It doesn't matter the actual value of the Guid that was passed.

It.Is<>构造对于方法的设置或验证很有用,可让您指定与参数匹配的函数.例如:

The It.Is<> construct is useful for setup or verification of a method, letting you specify a function that will match the argument. For instance:

Guid expectedGuid = ...
mockIRole.Setup(r => r.GetSomething(
                 It.Is<Guid>(g => g.ToString().StartsWith("4")), 
                 It.Is<Guid>(g => g != Guid.Empty), 
                 It.Is<Guid>(g => g == expectedGuid)))
         .Returns(ReturnSomething);

这使您不仅可以限制任何值,还可以宽容接受的东西.

This allows you to restrict the value more than just any value, but permits you to be lenient in what you accept.

设置(或验证)带有变量的方法参数时,是说您要精确该值.用另一个值调用的方法将永远不会与您的设置/验证相匹配.

When you set up (or verify) a method parameter with a variable, you're saying you want exactly that value. A method called with another value will never match your setup/verify.

Guid expectedGuids = new [] { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
mockIRole.Setup(r => r.GetSomething(expectedGuids[0], expectedGuids[1], expectedGuids[2]))
         .Returns(ReturnSomething);

现在只有一种情况,其中GetSomething将返回ReturnSomething:当所有Guid都与您为其设置的期望值匹配时.

Now there's exactly one case where GetSomething will return ReturnSomething: when all Guids match the expected values that you set it up with.

这篇关于为什么使用It.is&lt;或It.IsAny&lt;如果我只能定义一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆