【问题标题】:failed to run unit test for component that extends a base class in angular未能对以角度扩展基类的组件运行单元测试
【发布时间】:2020-01-01 09:42:55
【问题描述】:

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

component.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

我该如何解决这个问题?

【问题讨论】:

    标签: angular angular-unit-test


    【解决方案1】:

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

    获取服务实例

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

    监视 get 方法

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

    【讨论】:

    • 我试过了,还是报同样的错误,mock基类中的方法失败,有没有办法mock组件中的基类?
    • 发布您尝试过的组件和测试文件代码
    猜你喜欢
    • 1970-01-01
    • 2020-07-12
    • 2018-11-11
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    相关资源
    最近更新 更多