HAN
HAN

Reputation: 9

RhinoMock - Recursive mocking

I am not able to get the recursive mocking to work.

SomeClass someMOck = MockRepository.GenerateMock<SomeClass>();

//SomeClass is having another class as a property.

someMock.Stub(x => x.Manager.SomeData).Return("Hello");

During runtime it throws an error.

System.InvalidOperationException : Previous method 'SomeClass.get_Manager();' requires a return value or an exception to throw.

I am using 3.6 build 21.

Any Idea?

Upvotes: 0

Views: 567

Answers (1)

Wouter de Kort
Wouter de Kort

Reputation: 39888

This is because you don't have a value specified for someeClass.Manager.

You will need to mock the Manager class and set that as the return value for someeClass.Manager.

SomeClass someMock= MockRepository.GenerateMock<SomeClass>();
Manager managerMock= MockRepository.GenerateMock<Manager>(); 

managerMock.Stub(x => x.SomeData).Return("Hello");
someMock.Stub(x => x.Manager).Return(managerMock);

But another thing has to do with the design of your code. The Law Of Demeter states that you shouldn't use nested property.property in your code. You should only pass in the information that is really necessary and make sure that everything that is passed in is used. So is it really necessary to access a complete Manager object only to get to SomeData?

Upvotes: 2

Related Questions