Unit testing the services
In this section, you will learn how to write unit tests for services. Services are meant to be reused in one or more components. There can be different kinds of services, such as those providing utility APIs, reusable business rules/logic, and data processing tasks (also termed data services) such as retrieving data from the server. Components inject these services via the constructor, and later, use one or more APIs of the injected services to perform different tasks such as sending/retrieving data to/from the server. Externalizing the data handling logic in the services helps the components stay lean and easy to unit test, as the services can be mocked.
In this section, we will learn some of the following aspects of unit tests for data services that invoke one or more server APIs for data processing:
- Test double for HTTP
MockBackend
BaseRequestOptions
Service under test - DoctorService
For illustration purposes, we will consider DoctorService
, which has these two APIs...