【发布时间】:2011-10-15 21:04:08
【问题描述】:
使用 Rhino Mocks,如何确保在设置 mock 对象的 Expectations 时不调用方法。
在我的示例中,我正在测试 Commit 方法,我需要确保在执行提交时不调用 Rollback 方法。 (这是因为我在提交方法中有逻辑,如果提交失败会自动回滚)
下面是代码的样子..
[Test]
public void TestCommit_DoesNotRollback()
{
//Arrange
var mockStore = MockRepository.GenerateMock<IStore>();
mockStore.Expect(x => x.Commit());
//here i want to set an expectation that x.Rollback() should not be called.
//Act
subject.Commit();
//Assert
mockStore.VerifyAllExpectation();
}
当然,我可以在 Assert 阶段这样做:
mockStore.AssertWasNotCalled(x => x.Rollback());
但我想首先将其设置为期望。
【问题讨论】:
-
好奇为什么要使用 Expectation,而不仅仅是 AssertWasNotCalled?
-
@Cousken AssertWasNotCalled() 似乎不适用于 BackToRecord() 和 Replay(),也许这就是原因??
标签: c# unit-testing rhino-mocks