我可以对使用 Moles 创建的痣类型设定预期吗?

发布于 2024-11-08 16:39:40 字数 407 浏览 4 评论 0原文

我不仅需要交换实现,还需要添加必要的检查以确保以正确的顺序调用某些方法。我可以想象像 Mole + Mock 这样的东西会给我这个选择。有谁知道Moles有这个功能吗?

这段代码应该有帮助:

// Verify if Dispose was called
MDisposableObject.Constructor = delegate(DisposableObject instance)
{
    MDisposableObject mole = new MDisposableObject(instance);
    ...
    // This doesn't work 
    //objectContext.Expects(i => i.Dispose()).ToBeCalledOneTime();
};

I need to not only swap implementation but also to add necessary check to make sure that certain methods were called in the right order. I can imagine something like Mole + Mock would give me this option. Does anybody know if Moles has this feature?

This code should be helpful:

// Verify if Dispose was called
MDisposableObject.Constructor = delegate(DisposableObject instance)
{
    MDisposableObject mole = new MDisposableObject(instance);
    ...
    // This doesn't work 
    //objectContext.Expects(i => i.Dispose()).ToBeCalledOneTime();
};

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

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

发布评论

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

评论(1

蒗幽 2024-11-15 16:39:40

Moles 的目标是为所有内容提供存根(而不是模拟),即使是静态或密封方法。 Moles 手册中写道,它们不像其他模拟框架那样针对模拟方面:它们提供隔离,而不是模拟。如果你想检查你的鼹鼠的通话,你必须按照自己的方式去做。
例如:

    bool called = false;
    MDisposableObject.Constructor = (@this) =>
    {
        var mole = new MDisposableObject(@this)
        {
            Dispose = () =>
                {
                    Assert.IsFalse(called);
                    called=true;
                    //if you want to call the original implementation:
                    MolesContext.ExecuteWithoutMoles(() => (@this).Dispose());
                    //or do something else, even nothing
                }

        };
    };

只有 Typemock Isolator (功能强大但昂贵)和 JustMock(新并发,也不是免费的)为所有内容启用模拟功能。
如果您有一些接口、委托和虚拟方法,请使用免费的模拟框架,例如 Moq 或 RhinoMocks。

关于我的示例的警告:到目前为止,我还没有找到如何调用原始构造函数,我的意思是

var mole = new SDisposable();
(@this) = mole;
new MDisposable(mole) {...};

实际上,从我在 msdn 上读到的内容来看,这是不可能的...我希望以下版本能够实现这一点。

Moles aim to give stubs (and not mocks) for everything, even for static or sealed methods. It's written in the Moles manual that they are not aiming the mocking aspect like others mocking frameworks : they offer isolation, not mocks. If you want to check calls on your Moles, you have to do your own way.
For example:

    bool called = false;
    MDisposableObject.Constructor = (@this) =>
    {
        var mole = new MDisposableObject(@this)
        {
            Dispose = () =>
                {
                    Assert.IsFalse(called);
                    called=true;
                    //if you want to call the original implementation:
                    MolesContext.ExecuteWithoutMoles(() => (@this).Dispose());
                    //or do something else, even nothing
                }

        };
    };

Only Typemock Isolator (powerfull but expensive) and JustMock of Telerik (new concurrent, also not free) enable mocking features for everything.
If you have some interfaces, delegates and virtual method, use free mocking framework like Moq or RhinoMocks.

A warning about my example: until now I didn't found how to call the orignal constructor, I mean something like

var mole = new SDisposable();
(@this) = mole;
new MDisposable(mole) {...};

Actually, from what I read on msdn, it's not possible... I hope following releases will enable that.

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