使用 Rhino Mocks,我如何确保在模拟对象上设置 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();
}
当然,我可以在断言阶段这样做:
mockStore.AssertWasNotCalled(x => x.Rollback());
但我想首先将其设置为 Expectation。
最佳答案
另一种选择是:
mockStore.Expect(x => x.Rollback()).Repeat.Never();
关于c# - 如何设置 Expect 调用以检查 Rhino Mocks 中未调用的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6872204/