9

I have extended objects of type IDataReader with some extension methods that I needed. The problem is now when I try to mock the IDataReader, the extended method is not included in the mock so when the row Expect.Call(reader.ExtensionMethod()).Return(someValue) is reach the ExtensionMethod is executed which is not what I want! I want that call to be record and when the extension method is call from somewhere else I want it to return someValue.

Does anyone know how to get around this?

Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
  • 2
    possible duplicate of [How do I use Moq to mock an extension method?](http://stackoverflow.com/questions/562129/how-do-i-use-moq-to-mock-an-extension-method) – abatishchev Oct 21 '10 at 14:01
  • 1
    @abatishchev: I would say both yes and no :). The topic you're suggesting doesn't answer if it is possible or not for RhinoMock, and we also have the time aspect... someone could have found a clever solution to this in the last 18 months. – Tomas Jansson Oct 21 '10 at 14:14

4 Answers4

15

Disclosure: I work for Telerik.

Extension methods are in fact static methods concealed as instance methods. RhinoMock cannot mock static methods and there's no way you can do it, unless you use another mocking library, which uses a profiler.

Such a library is JustMock by Telerik.

Slavo
  • 15,255
  • 11
  • 47
  • 60
  • Ok... so how does this help me solve the problem with RhinoMock? – Tomas Jansson Oct 21 '10 at 14:12
  • 2
    It doesn't :) I'm sorry it's not possible with RhinoMock. RhinoMock intercepts calls to methods by inheriting from an object and overriding them (that's why they have to be virtual). You cannot override a static (extension) method and that's the reason Rhino fails in this situation. – Slavo Oct 21 '10 at 14:30
  • I currently know of two libraries that can do it - JustMock, which I've linked to in my reply, and TypeMock (http://www.typemock.com/). They both use a profiler and intercept method calls on a lower level, without relying on overriding. This performs slower, but does the job. – Slavo Oct 21 '10 at 21:41
  • 1
    Microsoft Fakes can shim the extension. – zaitsman Mar 08 '15 at 13:23
  • Единадесет години по-късно, този коментар все още е полезен. Благодаря ти, Славо ;) – TPAKTOPA Nov 23 '21 at 12:07
2

The answer seems to be no at the moment. To bad though, but I solved my problem with writing a mock class for my interface I wanted to mock instead. Since I didn't needed that many methods of the interface it went pretty fast.

Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
0

It is possible to stub extension method or any other static method without any framework. The following code allows you to do so, you just need to stub _doSumm.

public static class MyExtensions
{
    public static Func<int,int, int> _doSumm = (x, y) => x + y;

    public static int Summ(this int x, int y)
    {
        return _doSumm(x, y);
    }
}
dmigo
  • 2,849
  • 4
  • 41
  • 62
0

In my own instance, I wrapped the extension methods I had to stub into methods of a helper class that exposed the wrapper methods in a new interface. I switched my production code to have an instance of this helper class injected and changed the code to call the new methods. The unit tests are injecting a conveniently crafted stub of the helper interface.

While this solution is not perfect, it is still less dramatic than swapping the mocking framework.

eugen_nw
  • 147
  • 7