As far as I can see there are two ways, both with their drawbacks.
Get the object you are unit testing from the dependency injection system. This is low maintenance as you don’t have to manage anything when you change the framework around. But you are essentially testing the whole system from the point of view of your object, if a component fails it can blow up a lot of unit tests and it may not be apparent which one is failing.
is to manage the dependencies manually in the unit tests, and in some cases create test objects so that you can test each object in isolation. This keeps the unit tests discreet but dramatically increases the maintenance of the unit tests themselves. It also means that you don’t pick up on bugs cause by the way the objects interact on your live system.
Is either approach right or wrong? Should a compromise be used? Has anyone had any success stories either way.
If you're writing a unit test you should be using mocks for your dependencies and an IoC container shouldn't come into the picture. You should instantiate your class-under-test with mocks for the dependencies injected by hand.
If you're getting your object from the IoC container already wired up then what you're writing is integration tests which are very different.
Your goal for writing a unit test should be to write your test in isolation from the rest of the system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With