Sinon 不模拟为带参数的 GET 提供 URL
Sinon does not mock provided URL for GET with parameters
我正在尝试在 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 都可以正常工作
其实我忘了把response加入respondWith函数:)
server.respondWith('GET', `${CLIENT_API_ENDPOINT}/csr/states?vertical=${VerticalEnum.Federal}`, JSON.stringify(expectation));
它解决了问题
我正在尝试在 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 都可以正常工作
其实我忘了把response加入respondWith函数:)
server.respondWith('GET', `${CLIENT_API_ENDPOINT}/csr/states?vertical=${VerticalEnum.Federal}`, JSON.stringify(expectation));
它解决了问题