我正在尝试在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
请你指点一下这个问题,好像我错过了简单的问题,导致几乎相同的情况下,较短的网址工作正常
答案 0 :(得分:0)
实际上我忘了在responseWith函数中添加响应:)
server.respondWith('GET', `${CLIENT_API_ENDPOINT}/csr/states?vertical=${VerticalEnum.Federal}`, JSON.stringify(expectation));
它解决了问题