Mocking Static Methods Using Rhino.Mocks

Mocking Static methods using Rhino.Mocks

Is it possible to mock a static method
using Rhino.Mocks

No, it is not possible.

TypeMock can do this because it utilizes the CLR profiler to intercept and redirect calls.

RhinoMocks, NMock, and Moq cannot do this because these libraries are simpler; they don't use the CLR profiler APIs. They are simpler in that they use proxies to intercept virtual members and interface calls. The downside of this simplicity is that they cannot mock certain things, such as static methods, static properties, sealed classes, or non-virtual instance methods.

Mocking static methods in c# using Rhino Mock

What is the best approach for mocking out a static class in c#

The best approach is to hide those static calls behind abstractions. Those can be faked. For instance:

public interface IApplicationPhysicalPathProvider
{
string ApplicationPhysicalPath { get; }
}

public class AspNetApplicationPhysicalPathProvider : IApplicationPhysicalPathProvider
{
public string ApplicationPhysicalPath
{
get { return HostingEnvironment.ApplicationPhysicalPath; }
}
}

This advice holds for any mocking framework and even if you don't use a mocking framework at all.

How to mock extension methods with Rhino Mock?

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.

Stubbing a static extension method seems to work in Rhino Mocks unless I cast the return variable. Why?

Warning: this is really a suspicion more than anything else

The problem is that you're not really stubbing the extension methods at all - you're stubbing GetDouble in both cases.

I haven't looked at the code for Rhino Mocks for a while, but I suspect that the Stub method is basically saying:

  • Get ready for some calls on the mock!
  • Call the delegate passed in as an argument
  • Note which calls were made

That means you're effectively doing this:

canGetDecimal.Stub(ng => ng.GetDouble()).Return(1.2d);
canGetDecimal.Stub(ng => (decimal) ng.GetDouble()).Return(1.2m);

At that point, it would notice that you called GetDouble - but then you're trying to set the return value to 1.2m, which is invalid.

You could validate this pretty easily, with some logging. Add a log line to GetTheDoubleCastToDecimal and then split out the Stub call from the Return call:

Console.WriteLine("Before Stub");
var stubCall = canGetDecimal.Stub(obj => obj.GetTheDoubleCastToDecimal();
Console.WriteLine("After Stub");
stubCall.Return(1.2m);

I strongly suspect you'll find that whatever logging you add into the extension method is still logged between "Before Stub" and "After Stub" - showing that the extension method isn't being mocked out.

Moral: don't try to mock/stub extension methods. They're not polymorphic; they're just static methods, and it would be pretty tricky to fake them without deep wizardry. Only try to fake out genuinely polymorphic operations.

How to set Expect to a Extension method in Rhino Mocks 3.6

Extension methods are just syntatic sugar for static methods. So what you really need is the ability to mock the static method here. Unfortunately this is not possible in Rhino Mocks.

See the following StackOverflow thread for more details

  • Mocking Static methods using Rhino.Mocks

To mock a static method you need a mocking framework which actually uses the CLR profiler to intercept method calls. As the thread mentions TypeMock should be able to do this.

How do I unit test a method containing a static method?

It is not possible to mock a static method using Rhino Mocks.

However, it is possible to do this. Microsoft Moles and TypeMock have this capability.

I have tried using Moles, and it works alright. It generates a mock assembly for you, and you are able to replace a static method at runtime with a delegate.

Business functionality - RhinoMocks IEnumerable static method mocking

There is no business functionality to mocking the system IEnumerable methods. To Mock static methods however, you should create a virtual class that calls the static methods. You then mock the virtual class allowing yourself to "access" the static methods in test.

Mocking a method call generically

I don't think you can. When writing tests with rhino mocks, you need to follow the rules of the compiler, and avoiding to specify the generic type T, makes the compiler unhappy.

If you need to reuse that stub-code between multiple tests, each using different types for T, you can make a helper method as proposed here: Rhino Mocks: How to stub a generic method to catch an anonymous type?



Related Topics



Leave a reply



Submit