views:

3988

answers:

6

Is it possible to mock a static method using Rhino.Mocks ? if Rhino does not support this.. Is there a pattern or something which would let me accomplish the same ?

+10  A: 

Wrap the static method call in a virtual instance method in another class, then mock that out.

Rytmis
+3  A: 

The only mock framework that I know of that supports mocking statics is TypeMock.

As Rytmis suggested, you need to wrap the statics in something (i.e. an instance class with virtual methods or an interface) that you can then mock out.

Richard Banks
+24  A: 

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.

Judah Himango
+1  A: 

I have been mocking using moq , i dont think we can mock static members using this. because moQ creates a new proxy for the target (class or interface ) . so only the inheritable members (virtual in case of class, public in terms of interface ) can be mocked . clearly static members are not inherited. hence the problem.

vijaysylvester
+1  A: 

This is the biggest draw back to Rhino Mocks. I don't know that it is even possible for Rhino Mocks to have this implemented with out a re-concept of how it does its mocking.

Vaccano
+3  A: 

If you can't use TypeMock to intercept the method call, the recommended pattern to use is to create a proxy that forwards to the non-virtual or static methods you are interested in testing, then set the expectation on the proxy. To illustrate, consider the following classes.

class TypeToTest
{
    public void Method() { }
}

interface ITypeToTest
{
    void Method();
}

class TypeToTestProxy : ITypeToTest
{
    TypeToTest m_type = new TypeToTest();

    public void Method() { m_type.Method(); }
}

By creating this proxy, you can now use an ITypeToTest in place of where you were passing or setting a TypeToTest instance, making sure that the default implementation uses the TypeToTestProxy as it forwards to the real implementation. Then, you can create a mock ITypeToTest in your test code and set expectations accordingly.

Note that creating these proxies can be very tedious, error-prone, and time consuming. To address this I maintain a library and set of tools that generate assemblies containing these types for you. Please refer to this page for more information.

Steve Guidi