0

question: is it possible to clear the call history of a mock (or stub)?
( and with call history I don't mean the expected / recorded behaviour.)

The details:
I currently want am writing the following code with tests according the AAA syntax using NUnit and Rhino mocks.

public abstract class MockA
{
    private bool _firstTime = true;

    public void DoSomething()
    {
        if (_firstTime)
        {
            OnFirstDoSomething();
            _firstTime = false;
        }
    }

    public abstract void OnFirstDoSomething();
}

[TestFixture]
public class MockATest
{
    [Test]
    public void DoSomethingShouldSkipInitializationForSequentialCalls()
    {
        // Arrange
        var mockA = MockRepository.GeneratePartialMock<MockA>();
        mockA.Expect(x => x.OnFirstDoSomething()).Repeat.Any();
        mockA.DoSomething();  // -> OnFirstDoSomething() is called
        // here I want clear the call history of mockA 

        //Act
        mockA.DoSomething(); // -> OnFirstDoSomething should NOT be called
        mockA.DoSomething(); // -> OnFirstDoSomething should NOT be called

        //assert
        mockA.AssertWasNotCalled(x => x.OnFirstDoSomething());
    }
}

For the readability I always try to focus the calls in the Assert section on the changes that occur within the Act section.
However, the arrange section in this test, contains a (required) action that influence the call history of mockA.
As a result the assert fails.

I known I could catch a 'change' in the call history using the construction below, but it makes the expected behaviour of this test less readable.

{
    ...
    mockA.AssertWasCalled(x => x.OnFirstDoSomething(), opt => opt.Repeat.Once());
    //Act
    mockA.DoSomething();
    //Assert
    mockA.AssertWasCalled(x => x.OnFirstDoSomething(), opt => opt.Repeat.Once());
}

My question: is it possible to clear the call history of a mock (not recorded Expectations)?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
TJ Galama
  • 457
  • 3
  • 12

1 Answers1

0

You don't need to clear call history. Use Stub method instead of Expect in Arrange section:

[Test]
public void DoSomethingShouldSkipInitializationForSequentialCalls()
{
    // Arrange
    var mockA = MockRepository.GeneratePartialMock<MockA>();

    // this is what you have to change
    mockA.Stub(x => x.OnFirstDoSomething()).Repeat.Any();
    mockA.DoSomething();  // -> OnFirstDoSomething() is called
    // here I want clear the call history of mockA 

    //Act
    mockA.DoSomething();
    mockA.DoSomething();

    //assert
    mockA.AssertWasNotCalled(x => x.OnFirstDoSomething());
}
Petr Behenský
  • 620
  • 1
  • 6
  • 17
  • Unfortunatly this won't work. I added code of 'MockA' and some comment in the test to clear the expected behaviour. – TJ Galama Jul 04 '13 at 12:38
  • After several years of TDD , I decided to try-out another mock framewerk NSubstitute. One of the things I noticed is that the mocks call history can be cleared is I wanted with Rhinomock. – TJ Galama Jul 04 '16 at 19:58