Back to Developer Roadmap

Testing services with dependencies

src/data/roadmaps/angular/content/services-with-dependencies@TGRZBizDy83JKg_MhnRdX.md

4.01.2 KB
Original Source

Testing services with dependencies

When you add a dependency to your service, you must also include it in your tests. For isolated tests, pass an instance of the injectable dependency class into the service’s constructor. Using the inject function can add complexity. Injecting the real service is often impractical because dependent services can be difficult to create and control. Instead, mock the dependency, use a dummy value, or create a spy on the relevant service method. By using the TestBed testing utility, you can let Angular’s dependency injection handle service creation and manage constructor argument order.

Visit the following resources to learn more: