WYSIWYG

http://kufli.blogspot.com
http://github.com/karthik20522

Thursday, April 4, 2013

Moq, Unity and Servicelocator

Unit testing code that utilizes Unity, ServiceLocator with Moq framework is a thing of a beauty. Well,not really but a pain if it's your first time. Why? Because if you are a heavy google first, code second type of a developer then googling for Moq + ServiceLocator implementation would only give you Moq + Unity but no ServiceLocator implementation or examples. So, after spending nearly an hr, decided to share or at least remind myself how to for the next time:

Bootstrapping your ServiceLocator:
Note: This is the initialization of Unity in your code

Then in your calling method you would do something like this:

Following is how to moq the above Unity and ServiceLocator instance in Unit test cases:

Labels: ,