Testing Angular services - Mocking backend services

In the preceding section, you learned how to write test scripts to test our Angular services. In this section, we will write a test script and learn how to mock backend services in real-time projects.

The following are the use cases we will writing our test scripts for:

  1. Write a test script to test a method in service.
  2. Write a test script to check whether the return value of the method contains a particular value.


  1. Write a test script to mock the backend connection using mockBackend and also check whether the target URL is correct.
  2. Write a test script to set the mockResponse for the request URL.
  3. Finally, call a method written in service and map the response, which should be equal to the ...

Get Expert Angular now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.