【问题标题】:Sinon does not mock provided URL for GET with parametersSinon 不模拟带有参数的 GET 提供的 URL
【发布时间】:2018-05-14 14:03:12
【问题描述】:

我正在尝试在 Angular 应用中为我的服务开发测试,但遇到了一些问题。

这里是服务代码:

/**
 * Sends http request to get client states and territories available for specific vertical
 *
 * @param {number} vertical id of specific vertical. The following mapping is used:
 *    0 - Federal
 *    1 - State
 *    2 - Local
 *    3 - Education
 * @returns {Observable<State[]>} array of state objects with its display name and value as observable
 */
getClientsStatesByVertical(vertical: VerticalEnum): Observable<State[]> {
  let params = new HttpParams();
  if (vertical != null) {
    params = params.append('vertical', String(vertical));
  }
  return this.http
    .get(`${CLIENT_API_ENDPOINT}/csr/states`, {params: params}) as   Observable<State[]>;
}

这是我的测试:

it('#getClientsStatesByVertical with vertical provided', (done) => {
  const expectation = [{label: 'Georgia', value: 'GA'}];
  server.respondWith('GET', `${CLIENT_API_ENDPOINT}/csr/states?vertical=${VerticalEnum.Federal}`);
  const o = service.getClientsStatesByVertical(VerticalEnum.Federal);
  o.subscribe(response => {
    expect(response).toEqual(expectation);
    done();
  });
  server.respond();
});

我在使用 karma 运行此测试时收到以下错误:

HttpErrorResponse: Fake server request processing threw exception: Http failure response for http://localhost:19000/api/v1/clients/csr/states: 404 Not Found

您能否指出我的问题,似乎我缺少一些简单的东西,因为几乎相同的情况下较短的 URL 都可以正常工作

【问题讨论】:

    标签: angular typescript karma-runner sinon


    【解决方案1】:

    其实我忘了在 respondWith 函数中添加响应 :)

    server.respondWith('GET', `${CLIENT_API_ENDPOINT}/csr/states?vertical=${VerticalEnum.Federal}`, JSON.stringify(expectation));
    

    解决了问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多