如何在Angular中编写拦截器测试用例
我正在尝试测试Angular 6提供的HttpInterceptor。我已经根据示例创建了一个拦截器,但我无法为下面的拦截器编写测试用例,请指导我完成下面的问题 拦截器 错误如何在Angular中编写拦截器测试用例,angular,typescript,unit-testing,karma-jasmine,Angular,Typescript,Unit Testing,Karma Jasmine,我正在尝试测试Angular 6提供的HttpInterceptor。我已经根据示例创建了一个拦截器,但我无法为下面的拦截器编写测试用例,请指导我完成下面的问题 拦截器 错误 Chrome 75.0.3770(Windows 10.0.0)Lang-interceptor.service应包含内容类型标头失败 TypeError:无法读取null的属性“length” 在HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.H
Chrome 75.0.3770(Windows 10.0.0)Lang-interceptor.service应包含内容类型标头失败
TypeError:无法读取null的属性“length”
在HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.applyUpdate node_modules/@angular/common/fesm5/http.js:200:1)
在http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/common/fesm5/http.js:171:60
在Array.forEach()处
您的测试是正确的,因为您没有调用在预期时间之前进行http调用的服务
您还可以通过直接http调用扩展您的测试,它应该变成绿色
...
it('should include a Content-Type header', inject([HttpClient], (http: HttpClient) => {
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
}
http.get(environment.baseUrl + 'api/user/login', httpOptions).subscribe();
const httpRequest = httpMock.expectOne(environment.baseUrl + 'api/user/login');
// httpRequest.request.headers.delete(InterceptorSkipHeader);
expect(httpRequest.request.headers.has('Content-Type')).toBe(true);
expect(httpRequest.request.headers.get('Content-Type')).toBe('application/json');
httpMock.verify();
}));
...
两天后我就遇到了这个问题。服务拦截HTTP请求应该包括一个内容类型头FAILED TypeError:无法读取的属性'length'null@Krish你是对的,我忘了添加内容类型。检查我编辑的答案。让我检查并告诉结果我仍然面临问题。我用错误消息HMMM更新我的问题,你能用
httpRequest.request.headers.delete(InterceptorSkipHeader)取消行注释吗代码>。这是我在本地考试中唯一没有的。
describe('Lang-interceptor.service', () => {
let httpMock: HttpTestingController;
let interceptor: AutherizationInterceptor;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule, HttpClientTestingModule],
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: AutherizationInterceptor,
multi: true
}]
})
httpMock = TestBed.get(HttpTestingController);
interceptor = new AutherizationInterceptor();
});
it('should include a Content-Type header', inject([HttpClient], (http: HttpClient) => {
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
}
http.post(environment.baseUrl + 'api/user/login',null, httpOptions).subscribe();
const httpRequest = httpMock.expectOne(environment.baseUrl + 'api/user/login');
httpRequest.request.headers.delete(InterceptorSkipHeader);
expect(httpRequest.request.headers.has('Content-Type')).toBe(true);
expect(httpRequest.request.headers.get('Content-Type')).toBe('application/json');
httpMock.verify();
}));
Chrome 75.0.3770 (Windows 10.0.0) Lang-interceptor.service should include a Content-Type header FAILED
TypeError: Cannot read property 'length' of null
at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.applyUpdate node_modules/@angular/common/fesm5/http.js:200:1)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/common/fesm5/http.js:171:60
at Array.forEach (<anonymous>)
...
it('should include a Content-Type header', inject([HttpClient], (http: HttpClient) => {
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
}
http.get(environment.baseUrl + 'api/user/login', httpOptions).subscribe();
const httpRequest = httpMock.expectOne(environment.baseUrl + 'api/user/login');
// httpRequest.request.headers.delete(InterceptorSkipHeader);
expect(httpRequest.request.headers.has('Content-Type')).toBe(true);
expect(httpRequest.request.headers.get('Content-Type')).toBe('application/json');
httpMock.verify();
}));
...