Angular 角度:测试在可观察的最终确定之前完成
我试图测试一个Angular 角度:测试在可观察的最终确定之前完成,angular,jasmine,karma-runner,angular-test,Angular,Jasmine,Karma Runner,Angular Test,我试图测试一个拦截器,该拦截器在管道中添加了一个finalize操作符,但我无法让测试运行足够长的时间,以使返回的可观察对象(你知道,finalize)完成 以下是从intercept函数返回的observable: return next.handle(request).pipe( finalize(() => { console.log('int finalize'); this.activeRequests--; if (this.acti
拦截器
,该拦截器在管道中添加了一个finalize
操作符,但我无法让测试运行足够长的时间,以使返回的可观察对象(你知道,finalize)完成
以下是从intercept
函数返回的observable
:
return next.handle(request).pipe(
finalize(() => {
console.log('int finalize');
this.activeRequests--;
if (this.activeRequests === 0 ) {
this.loader.endLoading();
}
}));
我试图测试返回对象是否调用endload
beforeEach(() => spyOn(svc, 'endLoading').and.callThrough());
fit('calls endLoading', async(() => {
const expected = [];
client.get('/someUrl')
.pipe(
finalize(() => {
console.log('spec finalize');
expect(svc.endLoading).toHaveBeenCalledTimes(1);
})
).subscribe(response => expect(response).toEqual(expected));
http.expectOne('/someUrl').flush(expected);
expect(svc.endLoading).toHaveBeenCalledTimes(1);
}));
当测试执行时,控制台在
int finalize
之前输出spec finalize
,而不管我是使用async
、fakeAsync
还是done
。我怀疑将整个设置加载到组件存根中,并在nstable调用时链接,但似乎应该有更好的方法。有什么建议吗?您应该使用fakeAsync()
而不是async()
。然后,您可以使用flush()
或勾选(10000)
刷新等待的微任务。您还应该使用HttpClient的模拟模块。你能用done
等来展示你的解决方案吗。?可能你放错地方了。