RhinoMocks: Clear or reset AssertWasCalled ()

How can I verify that the layout is called in the "action" part of my test, ignoring any layout calls in the "organize" section of the test.

[Test]
public void ShouldOpenThrottleWhenDrivingHome()
{
    var engineMock = MockRepository.GenerateStub<IEngine>();
    var car = new Car(engineMock);
    car.DriveToGroceryStore(); // this will call engine.OpenThrottle

    car.DriveHome();

    engine.AssertWasCalled(e => e.OpenThrottle());
}

I would prefer not to try to insert a new layout or rely on .Repeat (), because then the test should know how many times the method is called in the setting.

+5
source share
2 answers

In these situations, I use the layout instead of the stub and combination Expectand VerifyAllExpectations:

//Arrange
var engineMock = MockRepository.GenerateMock<IEngine>();
var car = new Car(engineMock);
car.DriveToGroceryStore(); // this will call engine.OpenThrottle

engineMock.Expect(e => e.OpenThrottle());

//Act
car.DriveHome();

//Assert
engineMock.VerifyAllExpectations();

In this case, the wait is placed in the method after the layout is complete. Sometimes I think of it as my own testing style: Arrange, Expect, Act, Assert

+5
source

, , , - Arrange . , , WhenCalled. :

// Arrange
var engineMock = MockRepository.GenerateStub<IEngine>();
var car = new Car(engineMock);
int openThrotlleCount = 0;
engineMock.Expect(x => x.OpenThrottle(arg)).WhenCalled(invocation => openThrotlleCount++);
car.DriveToGroceryStore(); // this will call engine.OpenThrottle
var openThrottleCountBeforeAct = openThrotlleCount;

// Act
car.DriveHome();

// Assert
Assert.Greater(openThrotlleCount, openThrottleCountBeforeAct);

, ...

+1

All Articles