How to set the Expect call to check that a method is not called in Rhino Mocks

Santhosh picture Santhosh · Jul 29, 2011 · Viewed 12.8k times · Source

Using Rhino Mocks, how do I ensure that a method is not called while setting up the Expectations on the mock object.

In my example, I am testing the Commit method and I need to ensure that the Rollback method is not called while doing the commit. (this is because i have logic in the commit method that will automatically rollback if commit fails)

Here's how the code looks like..

[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();
}

Of course, I can do this at Assert phase like this:

mockStore.AssertWasNotCalled(x => x.Rollback());

But i would like to set this as an Expectation in the first place.

Answer

Pedro picture Pedro · Jul 30, 2011

Another option would be:

mockStore.Expect(x => x.Rollback()).Repeat.Never();