Mocking HTTP dependencies with Observables in Angular
In Angular, every HTTP call is represented as an Observable. That means whenever we are writing an integration test with an external service, we have to mock HTTP dependencies as we don’t want the real request being triggered from a test itself.
Getting ready
In this recipe, we’re going to test network request services that we implemented in Chapter 1 (https://github.com/PacktPublishing/RxJS-Cookbook-for-Reactive-Programming/tree/main/Chapter01/network-requests).
How to do it…
In order to mock HTTP calls to external services, we will take the help of the @angular/common/http/testing
module and learn how to test requests being sent in sequence as well as in parallel.
Step 1 – Setting up TestBed
To be able to mock HTTP communication with external services, we first need to set up the TestBed
module at the beginning of each test. In our recipe.service.spec.ts
file, we will have the...