0

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
    How did you stub the extension method.? From your example the extension method is just executed against the fake implementation of the interface, which is allowed. You may need to provide more source code for the workings of the extension method – Nkosi Oct 28 '16 at 10:40

1 Answers1

0

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.