有没有一种方法来测试服务的HTTP超时行为?
作业的超时时间设置为0,没有“超时”目前的记录时,我使用MockBackend均匀。
export class MyHttpService {
private sendGetRequest (job: HttpJob): Observable<any> {
return this.http.get(job.getUrl(), this.options)
.share()
.timeout(job.getTimeout(), () => this.requestTimeout(job))
.catch((err) => this.errorHandler(err, job));
};
private requestTimeout(job: HttpJob): Error {
job.errorCode = ErrorCode.TIMEOUT;
console.log('TIMEOUT');
return new Error('timeout');
}
...
测试(没有登录)
it('should resolve to TIMEOUT', () => {
job.timeout = 0;
let response: any;
service.sendRequest(job).subscribe(
(res: Response) => {
response = res.json();
console.log('OK', res);
},
err => {
response = err;
console.log('ER', err);
}
);
expect(job.errorCode).toEqual(ErrorCode.TIMEOUT);
});
谢谢!
更新 : 小例子 ,如果超时是注释掉,它会失败