首页 > 解决方案 > 未能对以角度扩展基类的组件运行单元测试

问题描述

我正在尝试为扩展基类的组件编写单元测试。

组件.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

我该如何解决这个问题?

标签: angularangular-unit-test

解决方案


您需要获取“MainStorageService”实例。然后 spyOn 为 'store.get('state')'。例如

获取服务实例

store = fixture.debugElement.injector.get(MainStorageService);

监视 get 方法

spyOn(store, 'get').and.callFake(() => {
    return Observable.from([]);
})

推荐阅读