문제

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