Microsoft Fakes - Stubbing an Extension Method Shouldn't Work But It Does

226 views Asked by At

I have an interface, ILoader, on which I have defined an extension method 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.

But I can stub the interface in 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)

1

There are 1 answers

0
BROTES DE GERMINADOS On

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.