Question

I would like to know how Rhino-Mocks finds out which method I want to stub. The example below stubs the method GetUserByName:

var stubUserRepository = MockRepository.GenerateStub<IUserRepository>();
stubUserRepository.Stub(x => x.GetUserByName("ayende")).Return(theUserObject);

In the example below I just forward a delegate to the Framework.

I think it is using method signature. Am I right?

Is it using reflection? If yes, how?

Where I can look for it? How it works?

I am using C# and the moment. I would like to know the concept behind and the C# applied stuff.

Was it helpful?

Solution

The Stub call sets up an optional Expect call. The function passed to Stub (x => x.GetUserByName("ayende"), in this instance) is actually executed against the mock object (which is a Castle DynamicProxy object). The call is intercepted and recorded in the MockRepository, with its parameters, as an expectation. An IMethodOptions<> object is generated from data saved about the call and returned, which Return(theUserObject) is subsequently called on.

You can use something like dotPeek to disassemble and examine Rhino.Mocks.dll in more detail; the relevant code is in RhinoMocksExtensions.cs, in the Rhino.Mocks namespace.

OTHER TIPS

Just want to add to the great Chris's answer the link to Rhino Mock source code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top