如何使用 Rhino.Mocks 评估类属性(getter 和 setter)
我正在研究 Rhino.Mocks 的工作原理,并试图了解如何在类属性中手动设置值。
我在互联网上看到过一个示例,其中您只需要 Property 作为 Expect.Call() 的参数,而不是使用方法。
MockRepository mocks = new MockRepository();
Person p = mocks.StrictMock<Person>();
Expect.Call(p.FirstName).Return("John");
Person 是一个类,例如:
public class Person
{
public string FirstName {get;set;}
}
我总是收到错误:
无效调用,最后一次调用已 使用过或者没有拨打电话(拨打 确保您正在调用虚拟 (C#)/可重写(VB)方法)。
我错过了什么吗?是否可以手动设置类 Properties 并评估它们以查看 getter 和 setter 是否工作正常?
I'm studying how Rhino.Mocks works and trying to understand how can I set manually a value in a class Property.
I have seen a sample in internet where you have only desired Property as argument of Expect.Call(), instead of using a method.
MockRepository mocks = new MockRepository();
Person p = mocks.StrictMock<Person>();
Expect.Call(p.FirstName).Return("John");
Person is a class such as:
public class Person
{
public string FirstName {get;set;}
}
I always receive the error:
Invalid call, the last call has been
used or no call has been made (make
sure that you are calling a virtual
(C#) / Overridable (VB) method).
Am I missing something? Is it possible to set manually class Properties and evaluate them to see if getters and setters are working fine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与任何模拟框架一样,Rhino Mocks 只能模拟定义虚拟方法和属性的接口或类。
这是因为在实现一个类时,Rhino 会根据您指定的类创建一个派生类,用使用拦截器的存根实现替换每个
虚拟
(或 VB 中的Overridable
)方法来处理呼叫。当您指定非虚拟方法时,Rhino 无法创建包装器。
对于
sealed
(VB 中的NonInheritable
)类来说也是如此。因此,为了让你的类正常工作,你应该这样实现该属性:
这样Rhino就可以相应地覆盖poperty。
As with any mocking framework, Rhino Mocks can only mock interfaces or classes that defines virtual methods and properties.
That's because when implementing a class, Rhino creates a derived class from the one you specify, replacing every
virtual
(orOverridable
in VB) method with a stub implementation that uses an interceptor to handle the call.When you specify a non virtual method, Rhino can't create a wrapper.
That is also true tor
sealed
(NonInheritable
in VB) classes.So for your class to work, you should implement the property as such:
This way Rhino can override the poperty accordingly.