Rhino Mocks AssertWasCalled (multiple times) on property getter using AAA

Confused picture Confused · Apr 8, 2009 · Viewed 23.2k times · Source

I have a mocked object that is passed as a constructor argument to another object.

How can I test that a mocked object's property has been called? This is code I am using currently:

INewContactAttributes newContact = MockRepository.GenerateMock<INewContactAttributes>();
newContact.Stub(x => x.Forenames).Return("One Two Three");
someobject.ConsumeContact(newContact);
newContact.AssertWasCalled(x => { var dummy = x.Forenames; });

This works except when within the "someobject" the getter on Forenames property is used multiple times. That's when I get "Rhino.Mocks.Exceptions.ExpectationViolationException: INewContactAttributes.get_Forenames(); Expected #1, Actual #2.."

Simply using

newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.Any());

does not work and gives the error below:

"The expectation was removed from the waiting expectations list, did you call Repeat.Any() ? This is not supported in AssertWasCalled()."

So how do I cater for the multiple calls?

Answer

Chris picture Chris · Jun 20, 2009

newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.AtLeastOnce());

Repeat.Any does not work with AssertWasCalled because 0 counts as any... so if it WASN'T called, the AsserWasCalled would return TRUE even if it wasn't called.