开发者

Unit testing with ServiceLocator

开发者 https://www.devze.com 2022-12-08 01:20 出处:网络
I am doing a unit test on a class that uses the unity dependency injection开发者_运维问答 framework.

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?


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.


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);
}


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.


You can always setup a Container+ServiceLocator and actually fulfill the required dependencies, for example, by registering mocks. See code examples #4 for how to setup a container/locator:

http://blogs.msdn.com/b/miah/archive/2009/05/12/servicelocator-and-unity-be-careful.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号