我正在使用 NgRedux 存储在角度组件中,以访问存储数据,如下所示:-
@select() currentQuiz$: Observable<IQuiz>;
currentQuiz: IQuiz;
// ...
ngOnInit() {
this.currentQuiz$.subscribe(currentQuiz => this.currentQuiz = currentQuiz)
}
但是我在运行基本测试时遇到错误无法读取未定义的属性'subscribe'
` it('should create', () => {
expect(component).toBeTruthy();
});`
解释后,我发现我收到此错误,因为 currentQuiz $ 属性在构造函数调用时未初始化(默认值为null),因此在调用 ngOnInit(),在这种情况下,我正在此属性上调用 subscribe()。尚未通过错误初始化。
在生产中运行良好。但是在运行测试用例时,出现了此错误。
有人知道如何解决此问题。