TDD is impossible without Inversion of Control and DIP
You must have heard of Inversion of Control (IoC), Dependency Inversion Principle (DIP), Dependency Injection (DI), IoC…
“IoC is all about inverting the control. To explain this in layman’s terms, suppose you drive a car to your work place. This means you control the car. The IoC principle suggests to invert the control, meaning that instead of driving the car yourself, you hire a cab, where another person will drive the car. Thus, this is called inversion of the control — from you to the cab driver. You don’t have to drive a car yourself and you can let the driver do the driving so that you can focus on your main work.”
Dependencies: an overview
If you were at any point confusing the terms dependency or dependent, an easy pointer to help remember: If A is dependent on B, then B is a dependency of A. Still confused?! I know I am(linguistics horror) Essentially anything that is required to run A is a dependency of A :).
So how is this important for TDD
Well you need loosely coupled things for TDD to be effective. And how do we get that? We get everything that is being used in a thing and move it to another method. So that’s deal, See, Inversion of Control is a principle, a paradigm, you can use it anywhere and everywhere. Not just with code :).
So if you have a data access layer(ex: a mongoose database layer) and a node.js business logic layer. Then you want to seperate out the data access parts in the business logic layer into a seperate class. Only then can you accurately and precisely separate concerns and then subsequently Test.
Sometimes i’m wordy, Check the linked article for a great example saying why (and how) your data access is probably tied to your business logic and why you should change it!
What’s DIP? I’ve got an article on that too!