我是使用Jasmine / Karma进行角度测试的新手,我在使用HttpTestingController测试角度服务时遇到了问题。这是源代码的一部分:
getProfile(userName: string) {
let config = {
params: {
user_id: "test"
}
}
return this.http
.get(`https://api.github.com/users/${userName}`, config);
}
使用HttpTestingController的expectOne调用服务时:
it('should add an Authorization header', () => {
let response;
userService.getProfile('blacksonic').subscribe(response => {
expect(response).toBeTruthy();
});
const req =
httpMock.expectOne({ method: 'GET', url:'https://api.github.com/users/blacksonic' });
});
我收到以下错误:
错误:预期一个匹配的条件请求“匹配方法:GET,URL:https://api.github.com/users/blacksonic”,找不到。在UserContext.eval的HttpClientTestingBackend.expectOne(./node_modules/@angular/common/fesm5/http/testing.js?:30:19)(./src/app/Interceptors/Interceptor.spec.ts?:85:28) )在ProxyZoneSpec.onInvoke的ZoneDelegate.invoke(./node_modules/zone.js/dist/zone.js?:387:26)(./node_modules/zone.js/dist/zone-testing.js?:287:39) )在Zone.run的ZoneDelegate.invoke(./node_modules/zone.js/dist/zone.js?:386:32)(./node_modules/zone.js/dist/zone.js?:137:43)at at UserContext.eval上的runInTestZone(./node_modules/zone.js/dist/zone-testing.js?:508:34)(。/ node_modules / zone.js / did / zone-testing.js?:523:20)
你正在传递一个参数。所以像这样的东西会起作用:
const req = httpMock.expectOne(
{ method: 'GET', url:'https://api.github.com/users/blacksonic?user_id=test' });