Angular 5 HttpClient HttpTestingController 测试错误路径
Angular 5 HttpClient HttpTestingController test error path
我有一个服务可以发出如下 HTTP get 请求
public get(uri: string, params?: HttpParams): Observable<Object> {
return params ? this._http.get(uri, { params: params }) : this._http.get(uri)
.catch((error: HttpErrorResponse) => this.catchHttpError(error));
}
private catchHttpError(error: HttpErrorResponse) : Observable<Object> {
this._logger.error(`error occurred: ${error.message}`);
throw(error);
}
我有一个可以测试成功路径的测试规范。我如何:
让HttpTestingController报错以便调用catchHttpError函数?
测试 _httpService.get 从 catchHttpError 函数返回错误?
到目前为止我的测试看起来像:
it('should make a get request and catch error', async(inject([HttpService, HttpTestingController], (_httpService: HttpService, backend: HttpTestingController) => {
spyOn(_httpService, 'get').and.callThrough();
let actualObjects: Object[] = [];
_httpService.get('/api/dummy/get/uri').subscribe((objects: Object[]) => {
actualObjects = objects;
});
backend.expectOne('/api/dummy/get/uri').flush(expectedObjects);
expect(_httpService.get).toHaveBeenCalledTimes(1);
expect(_httpService.get).toHaveBeenCalledWith('/api/dummy/get/uri');
expect(actualObjects).toEqual(expectedObjects);
expect(actualObjects.length).toBe(1);
//expect(_httpService.get).toThrow();
})));
结束了这个。
it('should make a get request and catch error', async(inject([HttpService, HttpTestingController], (_httpService: HttpService, backend: HttpTestingController) => {
spyOn(_httpService, 'get').and.callThrough();
let actualObjects: Object[] = [];
_httpService.get('/api/dummy/get/uri').subscribe(null, (response: HttpErrorResponse) => {
expect(response.message).toBe('Http failure response for /api/dummy/get/uri: 500 Server Error');
});
backend.expectOne('/api/dummy/get/uri').flush(expectedObjects, { status: 500, statusText: 'Server Error' });
expect(_httpService.get).toHaveBeenCalledTimes(1);
expect(_httpService.get).toHaveBeenCalledWith('/api/dummy/get/uri');
expect(actualObjects).toBeNull();
})));
我有一个服务可以发出如下 HTTP get 请求
public get(uri: string, params?: HttpParams): Observable<Object> {
return params ? this._http.get(uri, { params: params }) : this._http.get(uri)
.catch((error: HttpErrorResponse) => this.catchHttpError(error));
}
private catchHttpError(error: HttpErrorResponse) : Observable<Object> {
this._logger.error(`error occurred: ${error.message}`);
throw(error);
}
我有一个可以测试成功路径的测试规范。我如何:
让HttpTestingController报错以便调用catchHttpError函数?
测试 _httpService.get 从 catchHttpError 函数返回错误?
到目前为止我的测试看起来像:
it('should make a get request and catch error', async(inject([HttpService, HttpTestingController], (_httpService: HttpService, backend: HttpTestingController) => {
spyOn(_httpService, 'get').and.callThrough();
let actualObjects: Object[] = [];
_httpService.get('/api/dummy/get/uri').subscribe((objects: Object[]) => {
actualObjects = objects;
});
backend.expectOne('/api/dummy/get/uri').flush(expectedObjects);
expect(_httpService.get).toHaveBeenCalledTimes(1);
expect(_httpService.get).toHaveBeenCalledWith('/api/dummy/get/uri');
expect(actualObjects).toEqual(expectedObjects);
expect(actualObjects.length).toBe(1);
//expect(_httpService.get).toThrow();
})));
结束了这个。
it('should make a get request and catch error', async(inject([HttpService, HttpTestingController], (_httpService: HttpService, backend: HttpTestingController) => {
spyOn(_httpService, 'get').and.callThrough();
let actualObjects: Object[] = [];
_httpService.get('/api/dummy/get/uri').subscribe(null, (response: HttpErrorResponse) => {
expect(response.message).toBe('Http failure response for /api/dummy/get/uri: 500 Server Error');
});
backend.expectOne('/api/dummy/get/uri').flush(expectedObjects, { status: 500, statusText: 'Server Error' });
expect(_httpService.get).toHaveBeenCalledTimes(1);
expect(_httpService.get).toHaveBeenCalledWith('/api/dummy/get/uri');
expect(actualObjects).toBeNull();
})));