5

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)?

Zanecola
  • 1,394
  • 3
  • 15
  • 27

1 Answers1

12

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"});
meriton
  • 68,356
  • 14
  • 108
  • 175