【发布时间】:2020-02-24 17:53:41
【问题描述】:
我有以下代码,我在其中查找错误的自定义标头:
login(credentials: Credentials): Observable<any> {
return this.http.post(loginUrl, credentials)
.pipe(
catchError((httpErrorResponse: HttpErrorResponse) => {
let error = new Error('', httpErrorResponse.status);
...
if (httpErrorResponse.headers.get('customHeaderName')) {
error.message = 'Appropriate Response';
}
...
return throwError(error);
})
);
}
我正在使用HttpClientTestingModule 并尝试按如下方式进行测试:
it('should catch error with custom header', (done) => {
authService.login(credentials)
.subscribe({
next: null,
error: (error: Error) => {
expect(error.message).toEqual('Appropriate Response');
}
});
httpMock.expectOne({
url: apiUrl,
method: 'POST'
}).flush([], {status: 403, statusText: 'Forbidden', headers: {'customHeaderName': 'customHeaderValue'}});
done();
});
不确定问题出在哪里,但 status 和 statusText 按预期通过,但不包含标头,因此未激活 if 块。
有什么建议吗?
【问题讨论】:
标签: angular typescript unit-testing karma-jasmine angular-httpclient