Using spies on an injected service in a unit test
While you can provide stubs for your services in the unit tests with Jest, sometimes, it might feel like an overhead creating a mock for every new service. Let’s suppose that if the service’s usage is limited to one test file, it might make more sense to just use spies on the actual injected service. In this recipe, that’s exactly what we’re going to do.
Getting ready
The app that we are going to work with now resides in start/apps/chapter10/ng-test-services
inside the cloned repository:
- Open the code repository in your code editor.
- Open the terminal, navigate to the code repository directory, and run the following command to serve the project:
npm run serve ng-test-services
This should open the app in a new browser tab, and you should see the following:
Figure 10.8: ng-test-services app running on http://localhost:4200
Now that we have the app...