【问题标题】:TypeError: Cannot read property 'instance' of undefinedTypeError:无法读取未定义的属性“实例”
【发布时间】:2020-12-11 10:55:48
【问题描述】:

这是我要测试的方法:

showSnackbar(): void {
    if (this.modifiedReferences.length) {
      const snackbar = this.snackbarService.open({
        message: '',
        type: 'success',
        duration: 10000,
        actionText: 'SHOW DETAILS'
      });
      snackbar.instance.actionClicked.subscribe(() => {
        this.dialogService.modal(ModifiedComponent);
      });
    }
}

这是测试:

  it('should call the showSnackbar method', () => {
    const showSnackbarSpy: jasmine.Spy = spyOn(TestBed.inject(SnackbarService), 'open');

    component.showSnackbar();

    expect(showSnackbarSpy).toHaveBeenCalled();
  });

当我尝试测试时,它显示 zn 错误,说它无法读取未定义的属性“实例”。

【问题讨论】:

    标签: angular typescript testing


    【解决方案1】:

    对我来说,在我的TestBed.configureTestingModule() 解决此问题后添加.compileComponents()

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 2019-08-19
      相关资源
      最近更新 更多