简体   繁体   English

Microsoft假冒-扩展方法存根不起作用,但可以

[英]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.

相关问题 如何使用Microsoft伪造品模拟HttpSessionStateBase - How to use microsoft fakes to Mock HttpSessionStateBase 泛型接口的扩展方法不显示在子类上 - Extension method for generic interface does not show on subclasses Visual Studio 2012假货 - 如何验证方法被调用? - Visual Studio 2012 Fakes - How do I verify a method was called? MS伪造使用私有构造函数返回类的静态方法 - MS Fakes Static Method that Returns Class with Private Constructor 需要为使用C#在Microsoft单元测试工具中处理接口结果的类创建伪造品 - Need to create fakes for a class that manipulates the results of interface in microsoft unit testing tool using c# 将Moq C#代码转换为等效的c#Microsoft Fakes进行单元测试 - Converting Moq C# code to equivalent c# Microsoft Fakes for unit testing 用Moq测试私有方法不起作用 - Testing private method with Moq doesn't work 通用扩展方法测试 - Generic extension method testing Moq设置不适用于方法调用,后跟隐式转换 - Moq setup won't work for a method call followed by an implicit conversion 使用Moq的Stubed Unit of Work方法不返回预期的整数 - Stubed Unit of Work method with Moq doesn't return the expected integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM