2

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?

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
  • This feels a little odd. Why does `mock.MyMethod()` get called multiple times in the arrange section? Just so we understand more about your code. – Jason Evans May 17 '16 at 13:30

3 Answers3

2

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.

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
1

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

Mock.BackToRecord() will do this

Community
  • 1
  • 1
alexl
  • 6,841
  • 3
  • 24
  • 29
  • I don't think this clears the counts of the number of times a methods was called, only resets the stubbed return values. – Simon Keep Nov 08 '11 at 11:11
0
// 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();
Shankar S
  • 387
  • 5
  • 13