我创建了一个具有多个流依赖级别的组件。尝试为此页面编写单元测试时,我不断收到错误消息Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout
。寻找有关如何使我的观察对象调用的建议。
下面是该组件和相关测试的一些示例代码。
组件:
export class DashboardComponent implements OnInit {
public _foo = new Subject();
public foo$: Observable<IFoo[]> = this._foo.pipe(
tap(() => { this.loading = true; },
switchMap((id: number) => this.mySvc.getFoo(id)),
shareReplay(1)
);
public bar$: Observable<IBar[]> = this.foo$.pipe(
switchMap((foo: IFoo) => this.mySvc.getBar(foo.id),
shareReplay(1)
);
}
测试bar$
:
it('should get bar', done => {
component.foo$ = of(MockFooArray); // Set the dependency observable
jest.spyOn(component['service'], 'getBar').mockReturnValue(of(MockBarArray));
component.bar$.subscribe(() => {
expect.assertions(3);
// assertions
done();
});
});