Rhino 模拟抽象类而不模拟其虚拟方法?

发布于 2024-11-27 22:19:48 字数 135 浏览 1 评论 0原文

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

需要明确的是,我并不是想模拟虚拟方法的行为。我正在尝试/测试/虚拟方法(在模拟类上)。

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

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

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).

Is this idea a blatant misuse of Rhino Mocks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乙白 2024-12-04 22:19:48

是的,那应该绝对没问题。我不能说我已经尝试过,但如果失败的话我会感到非常惊讶。

编辑:我怀疑你想要 PartialMock 方法。这是一个例子:

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 时:

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...
    }
}

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

EDIT: 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
    }
}

EDIT: 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...
    }
}
奈何桥上唱咆哮 2024-12-04 22:19:48

您需要告诉 Rhino.Mocks 回调到原始实现,而不是执行仅拦截调用的默认行为:

var mock = MockRepository.GenerateMock<YourClass>();
mock.Setup(m => m.Foo()).CallOriginalMethod(OriginalCallOptions.NoExpectation);

现在您可以在 mock 上调用 Foo() 方法代码>对象。

You need to tell Rhino.Mocks to call back to the original implementation instead of doing its default behavior of just intercepting the call:

var mock = MockRepository.GenerateMock<YourClass>();
mock.Setup(m => m.Foo()).CallOriginalMethod(OriginalCallOptions.NoExpectation);

Now you can call the Foo() method on your mock object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文