[英]Why use It.is<> or It.IsAny<> if I could just define a variable?
Hi I've been using moq for a while when I see this code.嗨,当我看到这段代码时,我已经使用 moq 一段时间了。
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.我有三个参数,我刚刚在网上的一篇文章或博客中看到了这些参数。
What is the use of It.Is<>
or It.IsAny<>
for an object? It.Is<>
或It.IsAny<>
对对象有什么用? if I could use Guid.NewGuid()
or other types then why use It.Is
?如果我可以使用
Guid.NewGuid()
或其他类型,那么为什么要使用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.
但似乎这两种方式都没有错。
Using It.IsAny<>
, It.Is<>
, or a variable all serve different purposes.使用
It.IsAny<>
、 It.Is<>
或变量都有不同的用途。 They provide increasingly specific ways to match a parameter when setting up or verifying a method.在设置或验证方法时,它们提供了越来越具体的方法来匹配参数。
The method set up with It.IsAny<>
will match any parameter you give to the method.使用
It.IsAny<>
设置的方法将匹配您提供给该方法的任何参数。 So, in your example, the following invocations would all return the same thing ( ReturnSomething
):因此,在您的示例中,以下调用都将返回相同的内容(
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);
It doesn't matter the actual value of the Guid
that was passed.传递的
Guid
的实际值无关紧要。
The It.Is<>
construct is useful for setup or verification of a method, letting you specify a function that will match the argument. It.Is<>
构造对于方法的设置或验证很有用,让您可以指定与参数匹配的函数。 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);
Now there's exactly one case where GetSomething
will return ReturnSomething
: when all Guid
s match the expected values that you set it up with.现在只有一种情况
GetSomething
将返回ReturnSomething
:当所有Guid
与您设置的预期值匹配时。
If you look at the Quickstart documentation for Moq如果您查看Moq的 快速入门文档
Matching Arguments匹配参数
// any value
mock.Setup(foo => foo.DoSomething(It.IsAny<string>())).Returns(true);
// matching Func<int>, lazy evaluated
mock.Setup(foo => foo.Add(It.Is<int>(i => i % 2 == 0))).Returns(true);
// matching ranges
mock.Setup(foo => foo.Add(It.IsInRange<int>(0, 10, Range.Inclusive))).Returns(true);
// matching regex
mock.Setup(x => x.DoSomething(It.IsRegex("[a-d]+", RegexOptions.IgnoreCase))).Returns("foo");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.