【发布时间】:2019-12-13 19:07:10
【问题描述】:
我正在尝试对从另一个服务获取值(返回 Promise)然后执行 http GET 请求的服务进行单元测试。
我正在使用来自 @angular/common/http/testing 的 HttpTestingController 来模拟 http 请求。这是测试和产品代码:
tested.service.spec.ts
it('should get base url from indexedDb then execute http get request', (async () => {
fakeParametresService.getBaseUrl.and.returnValue(Promise.resolve({valeur: 'http://host:port'}));
testedService.get<any>('/endpoint').then((response: any) => {
expect(response.key).toBe('value');
});
await http.expectOne('http://host:port/endpoint').flush({key: 'value'});
http.verify();
}));
tested.service.ts
async get<T>(endpoint: string): Promise<T> {
const baseUrl = await this.parametresService.getBaseUrl();
return this.http.get<T>(`${baseUrl.valeur}${endpoint}`).toPromise();
}
似乎无法以这种方式模拟嵌套在承诺中的 http 请求。但也许这是我的一个误解。
【问题讨论】:
-
我对 rxjs 流也有类似的问题。
transmitted$.switchMapTo(this.initializePayment).switchMapTo(this.getPaymentUrl)。这些方法是私有的并进行 http 调用,流只是公共的......