Mock API Calls
Learn how to use HttpClientTestingModule to test HTTP requests.
We'll cover the following...
When we test an Angular component, we mock the data services and dependencies. We also test the service to make sure that it behaves correctly and makes the correct API calls. In most cases, we don’t want to actually call the external API, so we can make use of HttpClientTestingModule, which we provide as a mock instead of HttpClientModule. HttpClientTestingModule makes it easy to make assertions about the API calls we expect and also to return mock data where required.
In order to use HttpClientTestingModule, we need to do two things:
- Import
HttpClientTestingModulein ourTestBedconfiguration. - Fetch a copy of
HttpTestingControllerfrom theTestBeddependency injection.
Note: This technique works only when we use Angular’s
HttpClientModuleto make our HTTP ...
Ask