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

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

How can I verify that the mocked object property has been called? This is the code I'm currently using:

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, unless the getter on Forenames property is used multiple times in "someobject". This is when I get "Rhino.Mocks.Exceptions.ExpectationViolationException: INewContactAttributes.get_Forenames (); Expected # 1, Actual # 2 .."

Just using

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

does not work and gives an error below:

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

So, how do I handle multiple calls?

+48
c # properties unit-testing getter rhino-mocks
Apr 08 '09 at 9:38
source share
6 answers

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

Repeat.Any does not work with AssertWasCalled because 0 counts as any ... therefore, if it was NOT called, AsserWasCalled will return TRUE even if it was not called.

+70
Jun 19 '09 at 23:45
source share

I agree with the answer to Chris

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

Additionally, if you know exactly how many times a property will be called, you can do

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

where n is int.

+26
Jul 16 '10 at 12:16
source share

What is your motivation for checking the number of times it is called? Is this a particularly expensive operation? If so, then I would suggest that you put it in place of the method, because, semantically, properties should be inexpensive.

Also, checking the number of times you call a property is not a unit test pull (don’t worry that you have to check too much too often, we were all there). What you really need to test is that, assuming the state of your mock object, that the method generates the expected result. The number of calls made by this method does not really matter (unless it is a service for sending emails or something else). This is an implementation detail that you would not normally test, since a simple refactor would break your tests, since they would be too specific.

+2
Apr 08 '09 at 9:48
source share

Depending on your version of Rhino that you are using, you can use:

 // Call to mock object here LastCall.IgnoreArguments().Repeat.Never(); 
+2
Sep 07 '09 at 20:40
source share

newContact.Expect (c => c.ForeNames) .Return (...). Repeat.Any ()

0
Apr 08 '09 at 9:50
source share

From here :

 mock.AssertWasCalled(x => x.Name ="Bob"); 

or

 mock.AssertWasCalled(x => x.Name =Arg.Is("Bob")); 

or

 mock.AssertWasCalled(x => x.Name =Arg<string>.Is.NotNull); 
0
Oct 11 2018-11-11T00:
source share



All Articles