在模拟对象上调用预期方法后,如何立即通过单元测试?

发布于 2024-12-12 00:10:54 字数 87 浏览 0 评论 0原文

当使用带有模拟对象的 Rhino.Mocks 时:

有没有办法在调用预期方法后通过单元测试,而不执行调用该预期方法后的行?

谢谢

When working with Rhino.Mocks with a mock object in hand:

Is there a way to pass a unit-test once an expected method is called without executing the lines after the call to this expected method?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

辞取 2024-12-19 00:10:54

从RhinoMocks 3.5开始,你可以使用很好的 AssertWasCalled()

this.Service.BuldMessage("messageId");
this.Service.AssertWasCalled(x => x.GenerateMessage("messageId"), messageId));

编辑:评论的答案

RhinoMock 不负责更改测试执行流程,因此您必须使用 NUnit 断言,
Assert.Pass() 实用方法允许您立即结束测试,将其记录为成功:

if (this.Service.AssertWasCalled(...)))
{
   Assert.Pass("well done");
}

PS:正如其他人建议的那样,考虑重新设计单元测试,这迫使您执行此类条件测试退出。

Since RhinoMocks 3.5 you can use nice AssertWasCalled()

this.Service.BuldMessage("messageId");
this.Service.AssertWasCalled(x => x.GenerateMessage("messageId"), messageId));

EDIT: Answer to comment

RhinoMock is not in charge to change test execution flow, so you have to use NUnit asserts,
Assert.Pass() utility method allows you to immediately end the test, recording it as successful:

if (this.Service.AssertWasCalled(...)))
{
   Assert.Pass("well done");
}

PS: as others suggested consider redesing of unit test which forced you to do such conditional test exit.

溇涏 2024-12-19 00:10:54

您可以在 Rhino.Mocks 中使用 AssertWasCalled()

您期望如果调用该方法,测试应该停止,并且不应该运行以下几行,这意味着您编写了错误的测试。将断言(Nunit 或其他)作为测试的最后一行并拆分测试。您可能正在测试两件事,但不是单元测试。测试中不要有这种分支流程。测试中的所有行都应该被执行。理想情况下,每个测试应该只有一个断言。

按照您所期望的流程,当测试失败时,您也会失去从测试中获得的反馈。仅通过查看测试名称,您不知道什么失败了。

You can use the AssertWasCalled() in Rhino.Mocks

Your expectation that the test should stop if the method is called and should not run the following lines means that you have written the test wrong. Have the assert (Nunit or otherwise) as the last line of the test and split the test. You are probably testing two things and that is not unit testing. Don't have such kind of branching flow in tests. All the lines in the tests should be executed. And ideally, every test should have only one assert.

With the kind of flow you are expecting, you lose the feedback you get from the tests when they fail, as well. You don't know, by just looking at the test name, what failed.

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