如何使用 Moq 验证某个方法被调用了一次?
如何使用 Moq 验证某个方法被调用了一次? Verify()
与 Verifable()
的事情确实令人困惑。
How do I verify a method was called exactly once with Moq? The Verify()
vs. Verifable()
thing is really confusing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
Times.Once()
或Times.Exactly(1)
:以下是 Times 类:
AtLeast
- 指定应调用模拟方法的次数为最小次数。AtLeastOnce
- 指定模拟方法至少应调用一次。AtMost
- 指定应调用模拟方法的次数为最大值。AtMostOnce
- 指定模拟方法最多应调用一次。Between
- 指定应在 from 和 to 时间之间调用模拟方法。Exactly
- 指定模拟方法应该被精确调用几次。Never
- 指定不应调用模拟方法。Once
- 指定模拟方法应该被调用一次。只要记住它们是方法调用即可;我总是被绊倒,以为它们是属性,却忘记了括号。
You can use
Times.Once()
, orTimes.Exactly(1)
:Here are the methods on the Times class:
AtLeast
- Specifies that a mocked method should be invoked times times as minimum.AtLeastOnce
- Specifies that a mocked method should be invoked one time as minimum.AtMost
- Specifies that a mocked method should be invoked times time as maximum.AtMostOnce
- Specifies that a mocked method should be invoked one time as maximum.Between
- Specifies that a mocked method should be invoked between from and to times.Exactly
- Specifies that a mocked method should be invoked exactly times times.Never
- Specifies that a mocked method should not be invoked.Once
- Specifies that a mocked method should be invoked exactly one time.Just remember that they are method calls; I kept getting tripped up, thinking they were properties and forgetting the parentheses.
想象一下,我们正在构建一个计算器,其中包含一种将 2 个整数相加的方法。我们进一步想象一下需求是,当调用add方法时,调用一次print方法。以下是我们测试的方法:
这里是实际测试,代码中带有注释以供进一步说明:
注意:默认情况下,Moq 将在您创建 Mock 对象后立即存根所有属性和方法。因此,即使不调用
Setup
,Moq 也已经存根了IPrinter
的方法,因此您只需调用Verify
即可。然而,作为一个好的实践,我总是设置它,因为我们可能需要强制方法的参数以满足某些期望,或者强制方法的返回值以满足某些期望或调用它的次数。Imagine we are building a calculator with one method for adding 2 integers. Let's further imagine the requirement is that when the add method is called, it calls the print method once. Here is how we would test this:
And here is the actual test with comments within the code for further clarification:
Note: By default Moq will stub all the properties and methods as soon as you create a Mock object. So even without calling
Setup
, Moq has already stubbed the methods forIPrinter
so you can just callVerify
. However, as a good practice, I always set it up because we may need to enforce the parameters to the method to meet certain expectations, or the return value from the method to meet certain expectations or the number of times it has been called.测试控制器可能是:
并且当使用有效的 id 调用 DeleteCars 方法时,我们可以验证,通过此测试,服务删除方法只调用了一次:
Test controller may be :
And When DeleteCars method called with valid id, then we can verify that, Service remove method called exactly once by this test :