Rhino Mocks 模拟继承接口
我有一个类(例如 DerivedClass)继承自基类(例如 BaseClass)。 BaseClass 实现了一个名为 IBaseClass 的接口。 IBaseClass 有 1 个名为 TestProperty 的属性,它是一个整数自动属性。
我像这样的 PartialMultiMock DerivedClass:
derivedClassMock = repository.PartialMultiMock<DerivedClass>(typeof(IBaseInterface));
然后设置一个期望,如下所示:
derivedClassMock.Expect(d => d.TestProperty).Return(141);
但我不断收到以下异常:
“无效调用,已使用最后一个调用或未进行任何调用(确保您正在调用虚拟(C#) / 可重写 (VB) 方法)。”
如果我将 BaseClass 中 TestProperty 的实现标记为虚拟,则一切正常,但我试图理解为什么需要这样做。如果 DerivedClass 实现了 IBaseInterface,我不需要将其标记为虚拟来获取部分模拟功能。 (至少我认为不是 - 如果我错了,请纠正我)
然后我更进一步,尝试将多重模拟投射到 IBaseInterface 并设置期望,如下所示:
var derivedInterface = (IBaseInterface) derivedClassMock;
derivedInterface.Expect(d => d.TestProperty).Return(1);
此测试现在运行,没有异常,但值从 TestProperty 返回的不是预期的 1,而是 0,即 int 的默认值。这对我来说类似于存根的行为。
如果可能的话,有人可以解释一下,以帮助我在困惑时更好地理解这一点吗?我不能部分地多重模拟具有继承接口的类吗?为什么在接口上设置期望会表现出类似于存根的行为?
提前致谢。
I have a class e.g. DerivedClass that inherits from a base class e.g. BaseClass. BaseClass implements an interface called IBaseClass. IBaseClass has 1 property called TestProperty which is an integer auto property.
I PartialMultiMock DerivedClass like so:
derivedClassMock = repository.PartialMultiMock<DerivedClass>(typeof(IBaseInterface));
I then set an expectation as follows:
derivedClassMock.Expect(d => d.TestProperty).Return(141);
but I keep getting the following exception:
"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)."
If I mark the implementations of TestProperty in BaseClass as virtual, everything works but I'm trying to understand as to why this needs to be. If DerivedClass implemented IBaseInterface I wouldn't need to mark it as virtual to get the partial mock functionality. (at least i think not - please correct me if I'm wrong)
I then went a little further and tried to cast the multi mock to the IBaseInterface and set the expectation on that as follows:
var derivedInterface = (IBaseInterface) derivedClassMock;
derivedInterface.Expect(d => d.TestProperty).Return(1);
This test now runs without exceptions but the value returned from the TestProperty is not 1, as expected, but 0 i.e. the default value of int. This suggests to me behavior similar to a stub.
Can someone explain, if possible, to help me understand this a little better as I'm confused? Can I not partially multi mock a class with an inherited interface and why does setting the expectation on the interface exhibit stub like behavior?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上没有使用 PartialMultiMock,但在这种情况下,您似乎试图删除 DerivedClass 的返回值——它已将“TestProperty”实现为自动属性。由于这是一个带有设置器的自动属性,因此在我看来,您根本不需要对该属性进行存根。
如果你这样做了怎么办:
I haven't actually used PartialMultiMock but in this case, it seems that you are trying to stub out the return value of your DerivedClass -- which has implemented "TestProperty" as an auto-property. Since this is an auto-property with a setter then it seems to me that you would not require stubbing for this property at all.
What if you did: