如何检查在 Rhino Mocks 中是否使用 Expect 而不是 AssertWasNotCalled 调用 Create 方法?
如何在不使用 Rhino Mocks AssertWasNotCalled 方法的情况下检查 Create 是否未被调用。
这是测试:
[Test]
public void When_try_to_create_directory_that_already_exits_return_false()
{
var directoryInfoMock = MockRepository.GenerateMock<IDirectoryInfoWrap>();
directoryInfoMock.Stub(x => x.Exists).Return(true);
directoryInfoMock.Expect(x => x.Create());
Assert.AreEqual(false, new DirectoryInfoSample().TryToCreateDirectory(directoryInfoMock));
directoryInfoMock.VerifyAllExpectations();
}
另外,有人可以澄清 Stub 的作用吗?
How do I check if Create was not called without using the Rhino Mocks AssertWasNotCalled method.
Here is the test:
[Test]
public void When_try_to_create_directory_that_already_exits_return_false()
{
var directoryInfoMock = MockRepository.GenerateMock<IDirectoryInfoWrap>();
directoryInfoMock.Stub(x => x.Exists).Return(true);
directoryInfoMock.Expect(x => x.Create());
Assert.AreEqual(false, new DirectoryInfoSample().TryToCreateDirectory(directoryInfoMock));
directoryInfoMock.VerifyAllExpectations();
}
Also, can someone clarify what Stub does.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保对属性
directoryInfoMock.Exists
的任何调用都将返回true
。但如果该属性从未被调用或被多次调用,则不会导致测试失败。存根的目的是为测试中的代码提供一些帮助,使其能够正常运行。期望方法
directoryInfoMock.Create
至少被调用一次。如果没有,Rhino.Mocks 在执行directoryInfoMock.VerifyAllExpectations()
期间将抛出异常。所以基本上,您的单元测试应该按预期工作。测试的输出是什么?
更新:
您可能还想指定应该调用该方法的显式次数。这可以通过使用
Repeat.x
来完成,其中x
为Once()
、Twice()
、Never()
,或Times(N)
。这期望
Create
被永远调用。当然,如果实际被调用,您的测试将会失败。ensures that any call to the property
directoryInfoMock.Exists
will returntrue
. But if the property is never call or called many times, it will not cause the test to fail. The purpose of the stub is to provide some meal to your code under test so that it can run normally.expects that the method
directoryInfoMock.Create
be called at least once. If not, an exception will be thrown by Rhino.Mocks during the execution ofdirectoryInfoMock.VerifyAllExpectations()
.So basically, your unit test should work as expected. What is the output of the test?
UPDATE:
You might want to specify an explicit number of times the method should be called as well. This can be done by using
Repeat.x
withx
isOnce()
,Twice()
,Never()
, orTimes(N)
.This expects that
Create
is never called. And of course your test will fail if it is actually called.如果您需要确保仅调用您期望的方法,您可以考虑使用严格模拟。然后,当调用模拟中不需要的方法时,您将得到一个异常,对代码的唯一更改是在创建模拟时:
如果您确切地知道不应调用哪个方法,则最好使用 AssertWasNotCalled (您执行测试后使用它)。这样您就不会将测试与代码联系得如此紧密。
If you need to make sure that only the methods you expect are called you can consider using strict mocks. Then you will get an exception when a method was called that was not expected on your mock, the only change to your code is when you create your mock:
if you know exactly which method shouldn't be called its better to use AssertWasNotCalled (you use it after your test was executed). This way you don't tie your test with your code so closely.