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?
You can mock any extension method as you would do it with any other instance method. There is no need to add specific setup or use a dedicated API for extension methods. The Mock. Arrange() method will help you set up the behavior or expectations you need.
Extension methods in C# are the easiest and most under-used way to implement the Open-Closed Principle. The tricky part is to debug bugs caused by Extension methods. The best way to prevent these bugs is to ensure Unit Tests are written for Extension methods.
Rhino. Mocks supports three basic types of mock objects: Strict Mock. A strict mock requires you to provide alternate implementations for each method/property that is used on the mock.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With