Using a dependency injection system how do you unit test your code -


As far as I can see there are two ways, both with their own drawbacks.

  1. Obtain the object that you are testing the unit from the dependency injection system. It's less maintenance because when you change the structure, you do not have anything to manage. But you are essentially testing the entire system from the point of view of your object, if any component fails it can blow up many unit tests and it can not be obvious that someone is failing.

  2. Manually dependency is in managing unit tests, and in some cases create test objects so that you can test each object in isolation. This unit keeps track of the tests, but increases the maintenance of the unit dramatically, examines itself. It also means that the things you talk to on your live system do not raise the bugs due to it.

Is there a right or wrong approach? Should there be a compromise? Anyone had any success stories in any way

If you have unit If you are writing a test then you should use mocks for your dependencies and the IOC container should not be included in the photograph. You should also interact with your assurance with your class-under-test with a mock hand for injection dependency.

If you are getting your object from the IoC container, then it is already wired, then what you are writing is the integration test

< / P>


Comments