在Angular 5测试中,如何模拟http错误? [英] In Angular 5 testing, how to mock an http error?
本文介绍了在Angular 5测试中,如何模拟http错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Angular 5中进行单元测试.
I am doing unit test in Angular 5.
有了新的http模块,我知道我可以使用HttpTestingController
中的flush
方法来模拟响应数据,但是如何模拟错误(我想测试我的错误处理程序)?
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)?
推荐答案
使用error
而不是flush
httpTestingController.expectOne(url).error(errorEvent);
或将状态传递给刷新:
httpTestingController.expectOne(url).flush(null, {status: 400, statusText: "Bad Request"});
这篇关于在Angular 5测试中,如何模拟http错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文