angular - 如何使用 jest 和 angular 对商店 onInit 进行单元测试
问题描述
如何使用 jest 和 angular 对以下商店进行单元测试,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();
});
})
解决方案
您需要在.subscribe
回调方法中测试结果:
const valToCheck = ...
store
.pipe(select(studentfilesGetData), takeUntil(this.destroy$))
.subscribe(studentFiles => expect(valToCheck).toBe(component.getSortedEmployerFiles(studentFiles))
推荐阅读
- c - 为什么不能使用复合文字来分配已定义的、固定大小的数组?
- swiftui - 如何在 SwiftUI NavigationLink 中删除不透明动画
- python - 尝试进行 t 检验以查看不同投诉类型的平均响应时间是否相似
- django - 来自 npm-run-build 前端的 Django 中的静态文件抛出错误 404
- java - 具有两种语言的 TextToSpeech - Android
- c++ - 使用 VIVADO HLS 进行协同仿真
- date - MS Access:空时在日期字段中显示字符串
- python - 获取 Pandas 相关性的问题
- sql-server - 将 SQL Server 图形数据库中的 MATCH 子句和 INNER JOIN 与混合模型相结合
- java - 按来自另一个实体的连接列排序