Testing With Dependencies
Explore methods to unit test Angular components that depend on services by learning how to use stubs and spies. Understand how to isolate components from real dependencies, replace services with mocks, and use TestBed to manage service injection and overrides for thorough testing.
We'll cover the following...
In this lesson, we will look at a more advanced scenario for testing a component with dependencies.
In a real-world scenario, components are not usually as simple as AppComponent. They will almost certainly be dependent on one or more services. We have different ways of dealing with testing in such a situation. One thing is clear, though: if we are testing the component, then we should not test the service as well. So, when we set up such a test, the dependency should not be the real thing. There are different ways of dealing with that when it comes to unit testing; no solution is strictly better than another:
Stubbing: This is the method of telling the dependency injector to inject a stub of the dependency that we provide instead of the real thing.
Spying: This is the method of injecting ...