首页 文章

Sinon不会使用参数模拟为GET提供的URL

提问于
浏览
0

我正在尝试为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工作完全正常

1 回答

  • 0

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

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

    它解决了这个问题

相关问题