Microsoft Fakes是否支持垫片上的抽象方法?

Bar*_*pes 5 c# abstract microsoft-fakes

我有以下方式设置课程:

public abstract FooClass {
    public FooClass() {
        // init stuff;
    }

    public void RandomMethod() {
        // do stuff;
    }

    public abstract WhatIWantToShim();
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是在ShimFooClass上设置WhatIWantToShim,如下所示:

ShimFooClass.AllInstances.WhatIWantToShim = () => Boo();
Run Code Online (Sandbox Code Playgroud)

我可以设置RandomMethod就好了,

ShimFooClass.AllInstances.RandomMethod = () => CalculatePi();
Run Code Online (Sandbox Code Playgroud)

但是,生成的ShimFooClass似乎不会在ShimFooClass的AllInstances属性上创建WhatIWantToShim属性.

我看过http://msdn.microsoft.com/en-us/library/hh549176.aspx#bkmk_shim_basics,但我没有看到任何有关抽象方法的内容.我看到的唯一不支持的是终结器.有人知道这里发生了什么,是否支持这种情况?

Bar*_*pes 5

啊....无赖

接口和抽象方法。存根提供可用于测试的接口和抽象方法的实现。Shims 不能检测接口和抽象方法,因为它们没有方法体。

http://msdn.microsoft.com/en-us/library/hh549175(v=vs.110).aspx

更新:虽然可以做的是剔除垫片。

using (ShimsContext.Create())
{
    bool wasAbstractMethodCalled = false;
    var targetStub = new StubFooClass()
    {
        WhatIWantToShim01 = () => wasAbstractMethodCalled = true
    };
    var targetShim = new ShimFooClass(targetStub);
    targetShim.AllInstances.RandomMethod = () => CalculatePi();
    FooClass  target = targetShim.Instance;
    target.WhatIWantToShim();
    Assert.IsTrue(wasAbstractMethodCalled, "The WhatIWantToShim method was not called.");
}
Run Code Online (Sandbox Code Playgroud)

由于 shim 无法处理绕行 WhatIWantToShim 方法而存根可以,只需创建存根类的新实例并为抽象方法设置绕行处理程序。(注意:在我的实际代码中生成 Fakes 时,会自动为我添加 WhatIWantToShim 末尾的 01 标记)。

然后只需将实例化的存根传递给 shim 类的构造函数,并根据需要进行 shim 处理。