How to mock Angular 4.3 httpClient an error response in testing

The expectOne method in HttpTestingController class returns a TestRequest object. This TestRequest class has a flush method which can be used to deliver both successful and unsuccessful responses. We can resolve the request by returning a body along with some additional response headers (if any). Relevant info can be found here. Now, coming back to … Read more

How to cancel/unsubscribe all pending HTTP requests in Angular 4+

Checkout the takeUntil() operator from RxJS to globally drop your subscriptions : – RxJS 6+ (using the pipe syntax) import { takeUntil } from ‘rxjs/operators’; export class YourComponent { protected ngUnsubscribe: Subject<void> = new Subject<void>(); […] public httpGet(): void { this.http.get() .pipe( takeUntil(this.ngUnsubscribe) ) .subscribe( (data) => { … }); } public ngOnDestroy(): void { … Read more