为什么我的部分模拟都虚方法嘲笑,即使没有预期设定? [英] Why does my partial mock have all virtual methods mocked, even if no expectations are set?

查看:192
本文介绍了为什么我的部分模拟都虚方法嘲笑,即使没有预期设定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户的控制,做一些验证中,我想测试ValidateChildren方法。我已创建的用户控件的部分模拟,不过虽然我不是设置在ValidateChildren方法的任何期望,我只是调用它,它只是简单地跳过,并且方法中的code从不执行。要尝试和了解是怎么回事我创建了一个简单的测试,如下所示:

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();
    }
}

和使用它来测试它:

[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()是跨过步,我不能一步吧。

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.

任何想法,为什么?

推荐答案

好了,以后多一些玩耍(也许只是写这个问题倒使我更清楚地思考它)我想我已经找到了解决办法。

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.

看来我需要调用:

RhinoMocksExtensions.Replay (foo);

在我称之为

Assert.IsTrue (foo.Method2 ());

也许犀牛制品必须被明确告知,当虚拟方法正在使用,而不是设置的期望。不知道为什么。如果有谁知道这背后的道理我很想知道。

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.

这篇关于为什么我的部分模拟都虚方法嘲笑,即使没有预期设定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆