【问题标题】:How to test angular event listeners?如何测试角度事件监听器?
【发布时间】:2019-12-31 14:34:49
【问题描述】:

我有以下代码, 比较:

ngOninit() {
  window.addEventListner('oreantationChange' () =>  {
    this.sideNavbar();
  });
}

在这里我想覆盖上面的代码,以下是我的更改, 规格:

it ('-----',() => {
  spyOn(window,'addEventListner');
  window.addEventListner('oreantationChange' () =>  {
     expect(component.sideNavbar).toHaveBeenCalled()
  });
}

但是上面的测试没有得到覆盖。

谁能给我建议。谢谢。

【问题讨论】:

标签: angular typescript jasmine angular7 angular-unit-test


【解决方案1】:

除了将oreantationChange改成orientationChange之外,重写测试如下。

it ('-----', () => {
    spyOn(component, 'sideNavbar');
    window.dispatchEvent(new Event('orientationChange'));

    expect(component.sideNavbar).toHaveBeenCalled();
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 2018-03-17
  • 2014-11-16
相关资源
最近更新 更多