[英]Microsoft Fakes - Stubbing an Extension Method Shouldn't Work But It Does
I have an interface, ILoader
, on which I have defined an extension method CheckLoaderDatabaseConnection
: 我有一个接口
ILoader
,在该接口上定义了一个扩展方法CheckLoaderDatabaseConnection
:
//the extension method
public static class LoaderExtensions
{
public static void CheckLoaderDatabaseConnection(this ILoader loader)
{
//data access stuff
}
All the doumentation out there tells me I have to use shims when I want to stub an extension method because the method is static and it can't be stubbed. 所有的烦恼都告诉我,当我想对扩展方法进行存根时,必须使用垫片,因为该方法是静态的,因此无法存根。
True, it doesn't work in Moq because I've tried it. 是的,由于我已经尝试过,所以在Moq中不起作用。
But I can stub the interface in Fakes: 但是我可以在Fakes中对接口进行存根:
var loader = new MyNamespace.Fakes.StubILoader() { };
In my unit test, I pass in the stub to the constructor of the concrete instance I'm testing and when it gets to this line: 在单元测试中,我将存根传递到要测试的具体实例的构造函数以及到达此行的时间:
loader.CheckLoaderDatabaseConnection();
It calls the stubbed method (which does nothing) and works ok. 它调用存根方法(不执行任何操作),并且工作正常。
Why is this? 为什么是这样? I must be missing something.
我肯定错过了什么。 I haven't had to use shims here at all (though I can't stub it in Moq - when I try that, the real world extension is called & the whole thing blows up)
我根本不需要在这里使用垫片(尽管我无法在Moq中添加垫片-当我尝试该垫片时,称为真实世界的扩展名,整个过程都会崩溃)
No, the extension method wasn't getting invoked but after rebooting from a blue screen of death earlier the extension method is now getting invoked and the unit test is failing as I would expect. 不,扩展方法没有被调用,但是从死亡的蓝屏重启后,扩展方法现在被调用了,并且单元测试失败了。
Don't understand how this was working for several days though; 但是,不知道它如何运作了好几天; something weird & I don't think this question can be answered.
有点奇怪,我认为这个问题无法解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.