在模拟对象上调用预期方法后,如何立即通过单元测试?
当使用带有模拟对象的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从RhinoMocks 3.5开始,你可以使用很好的
AssertWasCalled()
编辑:评论的答案
RhinoMock 不负责更改测试执行流程,因此您必须使用 NUnit 断言,
Assert.Pass() 实用方法允许您立即结束测试,将其记录为成功:
PS:正如其他人建议的那样,考虑重新设计单元测试,这迫使您执行此类条件测试退出。
Since RhinoMocks 3.5 you can use nice
AssertWasCalled()
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:
PS: as others suggested consider redesing of unit test which forced you to do such conditional test exit.
您可以在 Rhino.Mocks 中使用
AssertWasCalled()
您期望如果调用该方法,测试应该停止,并且不应该运行以下几行,这意味着您编写了错误的测试。将断言(Nunit 或其他)作为测试的最后一行并拆分测试。您可能正在测试两件事,但不是单元测试。测试中不要有这种分支流程。测试中的所有行都应该被执行。理想情况下,每个测试应该只有一个断言。
按照您所期望的流程,当测试失败时,您也会失去从测试中获得的反馈。仅通过查看测试名称,您不知道什么失败了。
You can use the
AssertWasCalled()
in Rhino.MocksYour 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.