如何使用 jest 和 angular 对 store 进行单元测试,sonarqude 不包括这一行。谢谢回复
在我的 component.ts 中
public ngOnInit(): void {
this.store
.select(studentfilesGetData)
.pipe(takeUntil(this.destroy$))
.subscribe(studentFiles => {
this.studentFiles = this.getSortedEmployerFiles(studentFiles );
});
}
在我的 component.spec.ts 中
beforeEach(() => {
store = mock(Store);
component = new myComponent(instance(store));
when(store.select(studentfilesGetData)).thenReturn(of(studentFiles));
});
describe('ngOnInit', () => {
test('should create', () => {
expect(component).toBeTruthy();
});
})
答案 0 :(得分:1)
您需要在 .subscribe
回调方法中测试结果:
const valToCheck = ...
store
.pipe(select(studentfilesGetData), takeUntil(this.destroy$))
.subscribe(studentFiles => expect(valToCheck).toBe(component.getSortedEmployerFiles(studentFiles))
答案 1 :(得分:1)
也许 ngOnInit
没有在您的测试中运行。
尝试显式调用它:
test('should set studentFiles', () => {
component.ngOnInit();
expect(component.studentFiles).toBeTruthy(); // can do your own assertions
});