验证模拟的期望?

发布于 2024-11-01 03:14:48 字数 488 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简美 2024-11-08 03:14:48

当您执行单元测试时,您不仅测试了正在测试的组件的期望,还测试了正在测试的组件的期望以及它如何与它所依赖的其他组件交互嗯>。

假设您模拟了一个存储库&工作单元模式接口并将它们的模拟传递给您的组件。虽然如果您告诉存储库返回某些数据,组件可能会给出正确的结果,但您希望验证接口的实现是否按照您期望的方式调用。这就是验证的目的。

当与测试组件执行的处理结果相结合时,您不仅可以更明确地测试它将做什么,还可以测试它将如何与执行该操作所需的组件进行交互。

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.

江湖彼岸 2024-11-08 03:14:48

验证期望对于测试用例至关重要,就像断言语句对于测试一样重要。

您可以在测试方法中编写任意数量的没有 Assert 语句的代码,它都会通过。
但问题是 - “它测试什么吗?”

Assert 语句是测试用例的症结

类似地,Verify 方法是所有 Expectation 调用的关键,如果没有 verify 方法,您的测试用例就和没有 Assert 语句的测试用例一样好。

系统交互可以使用期望进行验证,这是一个三步过程

  1. 设置期望:让模拟框架知道您期望调用哪些交互。
  2. 交互或执行操作::执行要在 SUT(被测系统)上测试的实际调用
  3. 验证期望:要求模拟框架验证所有期望在执行步骤 2 时遇到。

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

  1. Setting Expectations: Letting know the mocking framework what interactions are you expecting to be invoked.
  2. Interact or Perform actions:: Perform the actual call which you want to test on the SUT(System Under Test)
  3. Verifying Expections: Asking the mocking framework to Verify all the expectations were met while performing Step 2.
清晨说晚安 2024-11-08 03:14:48

删除验证时,测试实际上并没有进行太多测试(除了可能生成的异常)。

本质上,您根本没有测试被测试对象与模拟的交互。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文