escargot agile
escargot agile

Reputation: 22379

Reset a mock in RhinoMocks

I'm writing NUnit tests with RhinoMocks. One of the tests looks like this:

mock = MockRepository<IFoo>.CreateMock();

// Arrange
// During the Arrange part, mock.MyMethod() gets called several times.

// Act
// During the Act part, mock.MyMethod() should be called exactly once.

// Assert
mock.AssertWasCalled(x => x.MyMethod()).Repeat.Once();

Naturally this fails because MyMethod() has been called more than once.

Is there a way I can reset the count of calls to MyMethod() before the Act part, so that only calls made after the reset are captured?

Upvotes: 2

Views: 2196

Answers (3)

alexl
alexl

Reputation: 6851

maybe this post can help you: How to clear previous expectations on an object?

Mock.BackToRecord() will do this

Upvotes: 1

Shankar S
Shankar S

Reputation: 405

// Arrange
// During the Arrange part, mock.MyMethod() gets called several times.

var mockRep = new MockRepository();
var mock = mockRep.dynamicMock<IFoo>();
Expect.Call(mock.MyMethod()).Return("desired result").Repeat.Time("count");

mock.Replay()

// Act
//test go here 

// Assert
mock.AssertWasCalled(x => x.MyMethod()).Repeat.Once();

Upvotes: 0

PatrickSteele
PatrickSteele

Reputation: 14677

I think @alexl's referenced SO question should help you out. But I'm curious as to what situation you have where your mocks are being called outside your Act phase of the test. It could be a sign of too tight of coupling between your objects.

As a possible workaround, if there's no state information preserved during Arrange, you could always just create another mock of IFoo that's only used during the Arrange phase.

Upvotes: 2

Related Questions