我是新手,正在学习Angular,并尝试为我的组件编写简单的单元测试。我的测试有时会失败,并出现以下错误:An error was thrown in afterAll TypeError: Cannot read property 'ids' of undefined。我也附上了错误追踪
这是我的组件实现:
ngOnInit() {
this.groupValueForm.valueChanges.subscribe((group: BasicGroup) => {
this.store.dispatch(GroupActions.selectGroup({ group: group }));
});
this.store.dispatch(GroupActions.getAllGroups());
this.store.pipe(select(selectAllGroups)).subscribe((basicGroups: BasicGroup[]) => {
this.groups = basicGroups;
});
}
这是我的测试类实现:
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
TopPanelModule,
STORE_MODULE_TEST
],
providers: [
{ provide: COUNTRY_ID, useValue: 'sv-SE' },
{ provide: TranslateService, useValue: mockTranslateService },
provideMockStore({ initialState })
]
});
store = TestBed.inject(MockStore);
fixture = TestBed.createComponent(TopPanelComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should use the groupList from store', (done: DoneFn) => {
spyOn(store, 'pipe').and.returnValue(of(groups));
component.ngOnInit();
store.pipe().subscribe(resp=>{
expect(component.groups).toEqual(groups);
done();
})
});
it('should dispatch getAllGroups', () => {
spyOn(component['store'], 'dispatch');
component.ngOnInit();
expect(component['store'].dispatch).toHaveBeenCalledWith(GroupActions.getAllGroups());
});
afterEach(() => {
fixture.destroy();
});
TopPanelComponent
中有ngOnDestroy
方法吗?能否请展示一下它的代码? - IAfanasovthis. destroy
。整个组件的代码可能有所帮助。您还可以尝试探索错误堆栈跟踪中的文件,那里可能会有一些线索。 - IAfanasovSTORE_MODULE_TEST
吗? - Buczkowski