angular - 未能对以角度扩展基类的组件运行单元测试
问题描述
我正在尝试为扩展基类的组件编写单元测试。
组件.ts
export class DesktopPresentationAreaComponent extends AbstractPresentationComponent {
constructor() {
super(store, webcast);
}
}
基类
export abstract class AbstractPresentationComponent {
constructor(protected store: MainStorageService,
protected webcast: WebcastService) {
this.store.get('state').subscribe();
}
当我运行“ng test”时,它显示以下错误。
TypeError: Cannot read property 'subscribe' of undefined in AbstractPresentationComponent
我该如何解决这个问题?
解决方案
您需要获取“MainStorageService”实例。然后 spyOn 为 'store.get('state')'。例如
获取服务实例
store = fixture.debugElement.injector.get(MainStorageService);
监视 get 方法
spyOn(store, 'get').and.callFake(() => {
return Observable.from([]);
})
推荐阅读
- flutter - Flutter Speech Recognition:如何获取单词正确发音与用户发音之间“相似度”的百分比
- material-ui - MUIv5 自动完成:如何在不重复名称的情况下定位子类?
- python-3.x - 无法在终端上执行 python3(PYTHONPATH 和 PATH 并发症)
- c# - 为什么一个文件中的一个 c# 类没有找到另一个文件/文件夹中定义的另一个类?
- mysql - MYSQL读取通讯包报错
- php - 如何发送变量?
- aws-lambda - AWS 事件桥 Lambda 调用
- sas - 每个月的 Proc sql 总和
- ssl - 我的 SSL 证书是否足以在我的购物车上进行金融交易
- php - 我可以通过 Id 获取特定的 JSON 值吗?