为什么我的部分模拟会模拟所有虚拟方法,即使没有设置期望?
我有一个用户控件,它在我想测试的 ValidateChildren 方法中进行一些验证。 我已经创建了用户控件的部分模拟,但是尽管我没有对 ValidateChildren 方法设置任何期望,但我只是简单地调用它,它只是被跳过,并且方法内的代码永远不会执行。 为了尝试理解发生了什么,我创建了一个简单的测试,如下所示:
public class Foo
{
public virtual bool Method1()
{
throw new NotImplementedException();
}
public virtual bool Method2()
{
return Method1();
}
}
并使用它来测试它:
[Test]
public void TestFooMethods ()
{
MockRepository m = new MockRepository();
Foo foo = m.PartialMock<Foo>();
RhinoMocksExtensions.Expect<Foo,bool>(
foo,
delegate (Foo obj)
{
return obj.Method1();
}
).Return(true);
Assert.IsTrue (foo.Method2());
}
现在我希望 foo.Method1 被模拟,而 foo.Method2 不被模拟。 但这总是返回 false,如果我尝试在调试器中单步执行,foo.Method2() 会被单步执行,并且我无法单步执行。
有什么想法吗?
I have a user control which does some validation in the ValidateChildren method which I would like to test. I have created a partial mock of the user control, but although I am not setting any expectations on the ValidateChildren method, I am simply calling it, it is simply skipped and the code inside the method never executes. To try and understand what is going on I created a simple test, like so:
public class Foo
{
public virtual bool Method1()
{
throw new NotImplementedException();
}
public virtual bool Method2()
{
return Method1();
}
}
and use this to test it:
[Test]
public void TestFooMethods ()
{
MockRepository m = new MockRepository();
Foo foo = m.PartialMock<Foo>();
RhinoMocksExtensions.Expect<Foo,bool>(
foo,
delegate (Foo obj)
{
return obj.Method1();
}
).Return(true);
Assert.IsTrue (foo.Method2());
}
now I would expect foo.Method1 to be mocked and foo.Method2 not to be. But this always returns false, and if I try and step through in the debugger foo.Method2() is stepped over, and I can't step in to it.
Any ideas why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您模拟一个对象,无论模拟的类型如何,它将覆盖所有抽象/虚拟方法。 不过,您可以做的是对您的方法做出期望,并告诉它执行它所覆盖的原始方法:
您没有按照设计方式使用Rhino Mocks,这也可能会给您带来麻烦。 我已经使用 C# 3.0 和 lambda 以及扩展方法按照应编写的方式重新编写了您的测试:
If you mock an object it will override all the abstract/virtual methods regardless of the type of mock. What you can do though is make an expectation on your method and tell it to execute the original method it is overriding by using:
You are not using Rhino Mocks the way it was design which could also be causing you trouble. I have re-written your test in the way it should be written using C# 3.0 and lambda and extension methods:
好吧,经过更多的尝试(也许只是写下问题可以帮助我更清楚地思考它)我想我已经找到了解决方案。
看来我需要调用:
在调用之前,
也许需要明确告知 Rhino 模拟何时使用虚拟方法而不是设置期望。 不知道为什么。 如果有人知道这背后的原因,我很想知道。
不管怎样,一切似乎都很顺利,真是幸福的日子。
Ok, after some more playing around (perhaps just writing the problem down helped me think more clearly about it) I think I have found the solution.
it seems that I need to call:
before I call
Perhaps Rhino mocks needs to be told explicitly when virtual methods are being used and not setting up expectations. Not sure why. If anyone knows the reasoning behind this I'd love to know.
Anyway everything seems to be working, so happy days.