You Should Use Dependency Injection for TDD

You Should Use Dependency Injection for TDD

Let see this code

class Car {
  whell = new Whell();
  run(){
    this.whell.move();
  }
}

How to make test to ensure the Car run whell correctly ?, above code is not possible to unit test. so we need to modify this code and create constructor to inject whell object.

class Car {
  constructor(whell){
    this.whell = whell
  }
  run(){
    this.whell.move();
  }
}

with this code we can inject mock whell to make sure this code run correctly.

it('Should run whell correctly', function() {
  const mockWhell = new MockWhell(); // example mock whell
  const car = new Car(mockWhell);
  car.run();

  assert(() => mockWhell.move(), shouldCalled(1)); // example
});

From the code above, we can conclude that, in order to create test-able code, we need access to other package injections. But it becomes a problem if there are many related packages, the constructor will be full of package objects. Therefore DI (Dependency injection) is a solution, object construction is controlled by the framework and developers can create the desired mock class in the unit test code.