Where to initialize container for MSTest project

May 22, 2013 at 9:08 PM
I am new to DI and Simple Injector and was trying to learn these technologies. I read in the documentation to avoid creating the container multiple times and just create it once. So does that mean that it needs to be initialized in the TestInitialize for MSTest or is there a better place to do it?
May 23, 2013 at 11:10 AM
The advice of creating one a single container instance only holds for your production application. For unit tests there is an other advice: don't use any IoC container at all.

Each unit test has its own special needs when it comes to which dependencies to supply to the class under test. Using a DI container in your unit tests therefore only complicates your tests and doesn't have any benefits. See more here.

For integration tests on the other hand, you might still want to use a DI container. In this case you can either use a single container for all integration tests or create one container per integration test, but important in this case is to reuse the DI configuration of the actual application, where you replace a few dependencies to prevent external systems from being accessed (mail services, sms proxies, web services, etc).

Note however that the focus in your application should be on writing unit tests. I believe I heard Robert Martin once say that your unit tests should cover between 70% and 100% of your code while integration tests should cover about 5% to 10% of code code base.
Marked as answer by dot_NET_Junkie on 2/26/2014 at 1:34 PM
May 23, 2013 at 2:00 PM
Thank you very much for guiding me in the right direction.