views:

326

answers:

3

I am doing a unit test on a class that uses the unity dependency injection framework.

This returns null: ServiceLocator.Current.GetInstance();

How can I get it to return a mock object or just the object itself?

A: 

Are you testing your core "DI integration" code? If not, your normal code should never (well, rarely) be interacting with your DI framework.

Normally your dependencies will be injected via constructor injection, and when testing, you can instead supply mock objects as those constructor dependencies. For example:

public class Foo {
    public Foo (IBar bar) {
        bar.Lift ();
    }
}

With the above code, you can simply mock IBar, and pass it to the Foo constructor.

Pete
+1  A: 

MSDN has this example that shows how to implement the service locator pattern with Unity. Essentially, you should pass the service locator object as a constructor argument of your class. This enables you to pass a MockUnityResolver, allowing you to take full control in a unit test.

[TestMethod]
public void InitCallsRunOnNewsController()
{
    MockUnityResolver container = new MockUnityResolver();
    var controller = new MockNewsController();
    container.Bag.Add(typeof(INewsController), controller);
    var newsModule = new NewsModule(container);

    newsModule.Initialize();

    Assert.IsTrue(controller.RunCalled);
}
Wim Coenen
A: 

You could make use of Poor Man's injection. Create a default constructor which retrieves the dependencies from the service locator, and forward those dependencies to a "real" constructor which takes them as parameters. That takes care of production situations.

Then when testing the class in question, pass in a fake/mock version of the dependencies to the "real" constructor, bypassing the default one altogether.

Grant Palin