Rhino 模拟一个抽象类而不模拟它的虚方法? [英] Rhino mock an abstract class w/o mocking its virtual method?

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

问题描述

我可以执行存在于使用 Rhino Mocks 模拟的抽象类上的虚拟方法的主体吗?

Can I execute the body of a virtual method that lives on an abstract class which has been mocked using Rhino Mocks?

明确地说,我不是要模拟虚方法的行为.我正在尝试/test/虚拟方法(在模拟类上).

To be clear, I'm not trying to mock the behavior of the virtual method. I'm trying to /test/ the virtual method (on the mocked class).

这个想法是对 Rhino Mocks 的公然滥用吗?

Is this idea a blatant misuse of Rhino Mocks?

推荐答案

是的,应该没问题.我不能说我已经尝试过了,但如果它失败了,我会感到非常惊讶.

Yes, that should be absolutely fine. I can't say I've tried it, but I'd be very surprised if it failed.

我怀疑您想要 PartialMock 方法.举个例子:

I suspect you want the PartialMock method. Here's an example:

using System;
using Rhino.Mocks;

public abstract class Abstract
{
    public virtual int Foo()
    {
        return Bar() * 2;
    }

    public abstract int Bar();        
}

class Test
{
    static void Main(string[] args)
    {
        MockRepository repository = new MockRepository();
        Abstract mock = repository.PartialMock<Abstract>();

        using (repository.Record())
        {
            Expect.Call(mock.Bar()).Return(5);
        }

        Console.WriteLine(mock.Foo()); // Prints 10
    }
}

或者在我第一次尝试 AAA 时:

Or in my first attempt at AAA:

using System;
using Rhino.Mocks;

public abstract class Abstract
{
    public virtual int Foo()
    {
        return Bar() * 2;
    }

    public abstract int Bar();        
}

class Test
{
    static void Main(string[] args)
    {
        // Arrange
        Abstract mock = MockRepository.GeneratePartialMock<Abstract>();
        mock.Stub(action => action.Bar()).Return(5);

        // Act
        int result = mock.Foo();

        // Assert
        mock.AssertWasCalled(x => x.Bar());
        // And assert that result is 10...
    }
}

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

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