두더지로 만든 두더지 유형에 대한 기대치를 설정할 수 있습니까?

StackOverflow https://stackoverflow.com/questions/6045769

  •  15-11-2019
  •  | 
  •  

문제

구현을 스왑 할뿐만 아니라 특정 방법이 올바른 순서로 호출되었는지 확인하는 데 필요한 수표를 추가해야합니다.나는 두더지 + 모크와 같은 것을 상상할 수 있습니다.아무도 몰릿 이이 기능을 가지고 있는지 알고 있습니까?

이 코드는 유용해야합니다.

// 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