Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Angular 5 testing, how to mock an http error?

Tags:

angular

I am doing unit test in Angular 5.

With the new http module, I know that I can mock response data by using flush method in HttpTestingController, but how can I mock an error (I want to test my error handler)?

like image 716
Zanecat Avatar asked Jan 08 '18 01:01

Zanecat


1 Answers

Use error rather than flush

httpTestingController.expectOne(url).error(errorEvent);

or pass a status to flush:

httpTestingController.expectOne(url).flush(null, {status: 400, statusText: "Bad Request"});
like image 146
meriton Avatar answered Sep 25 '22 13:09

meriton