[英]How Rhino-Mocks mocks?
I would like to know how Rhino-Mocks finds out which method I want to stub. 我想知道Rhino-Mocks如何找出我想存根的方法。 The example below stubs the method
GetUserByName
: 下面的示例对方法
GetUserByName
存根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. 我现在正在使用C#。 I would like to know the concept behind and the C# applied stuff.
我想知道背后的概念和C#应用的内容。
The Stub
call sets up an optional Expect
call. Stub
调用设置了可选的Expect
调用。 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). 传递给
Stub
的函数(在本例中为x => x.GetUserByName("ayende")
)实际上是针对模拟对象(它是Castle DynamicProxy对象)执行的。 The call is intercepted and recorded in the MockRepository
, with its parameters, as an expectation. 调用被拦截并记录在
MockRepository
,并带有其参数,这是期望值。 An IMethodOptions<>
object is generated from data saved about the call and returned, which Return(theUserObject)
is subsequently called on. 从有关调用的已保存数据中生成一个
IMethodOptions<>
对象,然后将其Return(theUserObject)
然后调用Return(theUserObject)
。
You can use something like dotPeek to disassemble and examine Rhino.Mocks.dll in more detail; 您可以使用诸如dotPeek之类的工具来更详细地分解和检查Rhino.Mocks.dll。 the relevant code is in
RhinoMocksExtensions.cs
, in the Rhino.Mocks
namespace. 相关代码位于
Rhino.Mocks
命名空间的RhinoMocksExtensions.cs
中。
只是想在Chris的回答中添加到Rhino Mock源代码的链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.