验证模拟的期望?
在 Rhino Mocks 文档中 它指出您必须验证模拟的期望,稍后必须使用VerifyAllExpectations() 或AssertWasCalled() 方法验证/断言该模拟。
但是,如果我注释掉验证,测试仍然通过。所以我想知道为什么你需要进行验证期望调用。
...
notificationSvc.Expect(o => o.UserIsLoggedOut());
...
//notificationSvc.VerifyAllExpectations();
In the documentation for Rhino Mocks it states that you must verify expectations on a mock which must be verified/asserted later using either the VerifyAllExpectations() or AssertWasCalled() methods.
However if I comment out the verification the test still passes. So I am wondering why you would need to have the verify expectation call at all.
...
notificationSvc.Expect(o => o.UserIsLoggedOut());
...
//notificationSvc.VerifyAllExpectations();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您执行单元测试时,您不仅测试了正在测试的组件的期望,还测试了正在测试的组件的期望以及它如何与它所依赖的其他组件交互嗯>。
假设您模拟了一个存储库&工作单元模式接口并将它们的模拟传递给您的组件。虽然如果您告诉存储库返回某些数据,组件可能会给出正确的结果,但您希望验证接口的实现是否按照您期望的方式调用。这就是验证的目的。
当与测试组件执行的处理结果相结合时,您不仅可以更明确地测试它将做什么,还可以测试它将如何与执行该操作所需的组件进行交互。
When you are performing the unit testing, you are not just testing the expectations of the component that you are testing, you are also testing the expectations of the component you are testing and how it interacts with other components it relies on.
Let's say that you mock a repository & unit of work pattern interfaces and pass mocks of them to your component. While the component might give you the right result if you tell the repository to return certain data, you want to verify that the implementations of the interfaces were called in the way that you expect them to. This is what verification is for.
When combined with testing the results of the processing your component does, you have a much more definitive test of not only what it will do, but how it will interact with the components that it requires to do it.
验证期望对于测试用例至关重要,就像断言语句对于测试一样重要。
您可以在测试方法中编写任意数量的没有 Assert 语句的代码,它都会通过。
但问题是 - “它测试什么吗?”
Assert 语句是测试用例的症结。
类似地,Verify 方法是所有 Expectation 调用的关键,如果没有 verify 方法,您的测试用例就和没有 Assert 语句的测试用例一样好。
系统交互可以使用期望进行验证,这是一个三步过程
Verifying an Expectation is as vital to a test case, as is an Assert statement is for a Test.
You can write any amount of code without Assert statements in a Test method, It would pass.
But the question is - "Is it Testing anything ?"
The Assert statement(s) are the crux of the Test Case.
Similarly the Verify methods are the crux of all Expectation calls, without Verify method your test case is as good as a Test case without an Assert statement.
A System interaction could be verified using Expectations, It's a three step proccess
删除验证时,测试实际上并没有进行太多测试(除了可能生成的异常)。
本质上,您根本没有测试被测试对象与模拟的交互。
When removing the Verify, the test isn't really testing much at all (other then possible exceptions that might get generated).
Essentially, you aren't testing the interaction of your tested object with your mock at all.