小编sam*_*mir的帖子

带有查询参数的 HttpTestingController.expectOne()

我是使用 Jasmine/Karma 进行角度测试的新手,我在使用 HttpTestingController 测试角度服务时遇到了一个问题。这是源代码的一部分:

getProfile(userName: string) {
    let config = {
      params: {
        user_id: "test"    
      }
    }
    return this.http
      .get(`https://api.github.com/users/${userName}`, config);
  }
Run Code Online (Sandbox Code Playgroud)

使用 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' });
Run Code Online (Sandbox Code Playgroud)

});

我收到以下错误:

Error: Expected one matching request for criteria "Match method: GET, URL: https://api.github.com/users/blacksonic", found none.
at HttpClientTestingBackend.expectOne (./node_modules/@angular/common/fesm5/http/testing.js?:301:19)
at UserContext.eval (./src/app/Interceptors/Interceptor.spec.ts?:85:28)
at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:387:26)
at ProxyZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:287:39)
at …
Run Code Online (Sandbox Code Playgroud)

testing karma-jasmine angular

6
推荐指数
1
解决办法
9313
查看次数

标签 统计

angular ×1

karma-jasmine ×1

testing ×1