Moq 中VerifyAll() 的用途是什么?
我在 阅读了这个问题: Moq 中 Verabilible() 的目的是什么? 我心里有一个问题:
Moq 中 VerifyAll()
的目的是什么?
I read the question at What is the purpose of Verifiable() in Moq? and have this question in my mind:
What is the purpose of VerifyAll()
in Moq?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
VerifyAll()
用于验证是否满足所有期望。假设您有:VerifyAll()
is for verifying that all the expectations have been met. Suppose you have:我会尝试完成@ema的回答,也许会给读者更多的见解。想象一下您有模拟对象,它是您的sut的依赖项。假设它有两种方法,您希望对它们进行设置,以免出现任何异常或为您的 sut 创建各种场景:
这就是 arrange 步骤。现在您想要运行一些您想要实际测试的方法(现在您行动):
现在您将使用
VerifyAll()
断言:什么你会在这里测试吗?您将测试您的设置方法是否被调用。在这种情况下,如果
Foo.Eat()
或Foo.Bark()
未被调用,您将收到异常并且测试将失败。因此,实际上,您混合排列和断言步骤。另外,您无法检查它被调用的次数,您可以使用.Verify()
来执行此操作(假设您有一些参数Param
,其属性名为Name< /code> 在您的
Eat()
函数中):I will try to complete @ema's answer, probably it will give more insights to the readers. Imagine you have mocked object, which is a dependency to your sut. Let's say it has two methods and you want to set them up in order to not get any exceptions or create various scenarios to your sut:
So that was arrange step. Now you want to run some method which you want to actually test(now you act):
Now you will assert with
VerifyAll()
:What you will test here? You will test whether your setup methods were called. In this case, if either
Foo.Eat()
orFoo.Bark()
were not called you will get an exception and test will fail. So, actually, you mix arrange and assert steps. Also, you cannot check how many times it was called, which you can do with.Verify()
(imagine you have some parameterParam
with property calledName
in yourEat()
function):