【问题标题】:Can I set expectations on mole types created with Moles?我可以对使用 Moles 创建的痣类型设定期望吗?
【发布时间】:2011-08-28 02:17:10
【问题描述】:

我不仅需要交换实现,还需要添加必要的检查以确保以正确的顺序调用某些方法。我可以想象像 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();
};

【问题讨论】:

    标签: c# .net moles pex-and-moles


    【解决方案1】:

    Moles 旨在为所有内容提供存根(而不是模拟),即使是静态或密封方法。在 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
                    }
    
            };
        };
    

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

    关于我的示例的警告:直到现在我还没有找到如何调用原始构造函数,我的意思是类似

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

    实际上,根据我在 msdn 上阅读的内容,这是不可能的...我希望后续版本能够实现这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 2010-10-13
      • 2010-12-27
      • 1970-01-01
      相关资源
      最近更新 更多