Spying on the Dependency Method
Explore how to isolate and test specific methods of dependencies in Angular using Jasmine spies. Understand setting up spies on real or fake service methods, monitoring call counts, arguments, and returning mock data to write precise unit tests.
We'll cover the following...
We studied how stubbing a dependency is very simple, but it is not always possible. Using a stub is not the only way to isolate logic in a unit test. We don’t have to replace the entire dependency—only the parts our component uses. Replacing certain parts means we point out specific methods on the dependency and assign a spy to them. A spy can answer what we want it to answer, but we can also see how many times it was called and with what arguments. So, a spy gives us much more information about what is happening.
There are two ways to set up a spy in a dependency:
Inject the actual dependency and spy on its methods.
Use the ...