Rhino - 模拟类而不是覆盖虚拟方法 [英] Rhino - Mocking classes and not overriding virtual methods

查看:35
本文介绍了Rhino - 模拟类而不是覆盖虚拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我模拟一个类,如下所示,有什么方法可以让模拟覆盖虚拟方法?我知道我可以简单地删除 virtual 修饰符,但我实际上想稍后删除此方法的行为.

If I'm mocking a class, like below, is there any way I can get the mock to not override a virtual method? I know I can simply remove the virtual modifier, but I actually want to stub out behavior for this method later.

换句话说,除了删除虚拟修饰符之外,我如何才能通过此测试:

In other words, how can I get this test to pass, other than removing the virtual modifier:

namespace Sandbox {
    public class classToMock {
       public int IntProperty { get; set; }

       public virtual void DoIt() {
           IntProperty = 1;
       }
}

public class Foo {
    static void Main(string[] args) {
        classToMock c = MockRepository.GenerateMock<classToMock>();
        c.DoIt();

        Assert.AreEqual(1, c.IntProperty);
        Console.WriteLine("Pass");
    }
}

}

推荐答案

您想使用 部分模拟,它只会在您创建期望时覆盖该方法:

You want to use a partial mock, which will only override the method when you create an expectation:

classToMock c = MockRepository.GeneratePartialMock<classToMock>();
c.DoIt();

Assert.AreEqual(1, c.IntProperty);

这篇关于Rhino - 模拟类而不是覆盖虚拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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