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.

有帮助吗?

解决方案

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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top