首页 > 解决方案 > 角度 6 单元测试

问题描述

我正在开展一个项目,在该项目中,我从多个 3rd 方 api 获取报告数据,并将其显示在带有自定义过滤器和其他内容的表中。它工作正常,但我在测试时遇到问题。我有 1 个 json 文件,其中包含每个面板的凭据和端点。应用程序启动后,我立即读取该文件。

// factory that reads file
export function akrpFactory(akrp: AkrpService) {
    return () => akrp.load();
}
// akrp.load method returns this.http.get("./assets/data.json")
// inside ngModule
providers: [
    {
        provide: APP_INITIALIZER,
        useFactory: akrpFactory,
        deps: [AkrpService],
        multi: true
    }
]
// after this data is available throughout the application

在仪表板组件 ngAfterViewInit 中有一个 http 调用

ngAfterViewInit() {
    this.getPerformingFeeds(this.selectedPanel.key, '0-100');
}
// this.selectedPanel.key contains one of data records from json file
// here I am getting error ('TypeError: Cannot read property 'key' of undefined') during testing

在 .spec.ts 文件中只有一个测试用例。我试图分配 component.selectedPanel 但仍然是同样的错误

it('should create', () => {
    expect(component).toBeTruthy();
});

好吧,我是 Angular 2+ 版本的新手,并且还在测试,不知道该怎么做。任何事情都会有帮助,谢谢。

标签: angularjasmineangular-test

解决方案


推荐阅读