Reputation: 577
sorry for what might be a poor question but I am a bit confused.
Would TDD help ensure DI? or is it the other way around (DI is required for TDD).
Upvotes: 3
Views: 1883
Reputation: 14064
This is actually a very good question.
I would say nothing in TDD's DNA makes it intrinsically related to Dependency Injection. It just so happens that the Refactoring step in TDD sometimes leads to extracting behavior to an external dependency, which can then optionally be injected into the class under test via its constructor. Nothing systematic though, how to manage dependencies is more a matter of the refactorer's own taste.
The reverse is also true - nothing in DI forces you to write your code with a TDD approach. DI improves testability indeed but that doesn't necessarily imply test-first nor the Red-Green-Refactor cycle.
However, later refinements/specializations of TDD like Outside-In TDD do make intensive use of Dependency Injection, and TDD practitioners are also typically proponents of SOLID principles which include Dependency Inversion at their core. Popular tools like isolation and auto-mocking frameworks combine TDD and DI as well. This may be why the 2 practices are often seen used and talked about together.
Upvotes: 3
Reputation: 64213
TDD is a method to design and develop a software.
Dependency injection is a technique to allow selection among multiple implementations of a given dependency interface at run time or at compile time.
While one might argue that these two are independent, doing TDD without DI is possible, but difficult. Therefore, I would say that doing TDD for the medium to large projects does require dependency injection.
Upvotes: 4
Reputation: 20320
Neither. TDD should help design better software. A better design might use the inversion of control pattern
DI is an implementation of the IOC pattern
Upvotes: 2