角度6单元测试

时间:2018-12-19 07:46:33

标签: angular jasmine angular-test

我正在一个项目中,在该项目中,我从多个第三方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+版本的新手,并且还在测试,不确定该怎么做。一切都会有帮助的,谢谢。

0 个答案:

没有答案