我不仅需要交换实施,还需要添加必要的检查,以确保以正确的顺序调用某些方法。我可以想象像鼹鼠+模仿会给我这个选项。有人知道莫尔斯是否有这个功能?

此代码应该有用:

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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top