Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:测试在可观察的最终确定之前完成_Angular_Jasmine_Karma Runner_Angular Test - Fatal编程技术网

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
等来展示你的解决方案吗。?可能你放错地方了。