Могу ли я установить ожидания на мольных типах, созданных с молями?

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