Angular 测试,如何检查没有进行 Rest 调用?
Angular testing, how to check that there was no Rest call made?
我有以下方法
getWsById(idWs: number, isCache: boolean = true): Observable<WSModel> {
try {
if (isCache) {
const answer: WSModel = this.fullWSModelCache$
.getValue()
.filter((fullWS) => fullWS.workspace.workspaceId === idWs)
.map((fullWS) => fullWS.workspace)[0];
return of(answer);
}
const params = new HttpParams().set('idWs', idWs.toString());
const wsResponse = this.http.get<WSModel>('rest/ws/ws-by-id', {
params
});
return wsResponse;
} catch (error) {
this.updateHasError(true);
console.error(`Error occurred: ${error}`);
}
}
我想编写一个单元测试来检查缓存模式并验证没有 Rest 调用。
如何做到这一点?一点想法都没有!!!
我目前的测试如下
it("should test getWsById cache mode", () => {
wsService.updateFullWsModelCache(currentFullWSModelCache);
wsService.getWsById(testingWsModel.workspaceId).subscribe( res => expect(res).toEqual(testingWsModel));
const req = httpTestingController.expectOne({
method: 'GET'
});
//expect(req.countOfCalls).toEqual(0); how to achieve this
});
我将重构我的服务方法,在 function/method 中提取缓存实用程序,在另一个 function/method
中提取 Rest 调用实用程序
然后会检查method/function是否被调用
我有以下方法
getWsById(idWs: number, isCache: boolean = true): Observable<WSModel> {
try {
if (isCache) {
const answer: WSModel = this.fullWSModelCache$
.getValue()
.filter((fullWS) => fullWS.workspace.workspaceId === idWs)
.map((fullWS) => fullWS.workspace)[0];
return of(answer);
}
const params = new HttpParams().set('idWs', idWs.toString());
const wsResponse = this.http.get<WSModel>('rest/ws/ws-by-id', {
params
});
return wsResponse;
} catch (error) {
this.updateHasError(true);
console.error(`Error occurred: ${error}`);
}
}
我想编写一个单元测试来检查缓存模式并验证没有 Rest 调用。 如何做到这一点?一点想法都没有!!!
我目前的测试如下
it("should test getWsById cache mode", () => {
wsService.updateFullWsModelCache(currentFullWSModelCache);
wsService.getWsById(testingWsModel.workspaceId).subscribe( res => expect(res).toEqual(testingWsModel));
const req = httpTestingController.expectOne({
method: 'GET'
});
//expect(req.countOfCalls).toEqual(0); how to achieve this
});
我将重构我的服务方法,在 function/method 中提取缓存实用程序,在另一个 function/method
中提取 Rest 调用实用程序然后会检查method/function是否被调用