Question

I have something like this:

public class SomeClass
{
    protected ISomeInterface SomeProperty
    {
        get { return SomeStaticClass.GetSomeInterfaceImpl(); }
    }

    public void SomeMethod()
    {
        // uses SomeProperty in calculations
    }
}

How can I test SomeMethod, mocking SomeProperty using Rhino Mocks? I was thinking about getting the accessor, rewrite the accessor using IL, just to return the mock proxy. How crazy that sounds?

Was it helpful?

Solution

You can't mock class under test but only dependencies. So if you use some kind of factory instead of SomeStaticClas and inject it using constructor parameter of SomeClass you can then mock the factory class.

public class SomeClass
{
    public SomeClass(ISomeInterfaceFactory factory)
    {
        this.factory = factory;
    }

    protected ISomeInterface SomeProperty
    {
        get { return factory.GetSomeInterface(); }
    }

    public void SomeMethod()
    {
        // uses SomeProperty in calculations
    }
}

public interface ISomeInterfaceFactory
{
    ISomeInterface GetSomeInterface();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top