【问题标题】:How to catch $broadcast in spyOn?如何在 spyOn 中捕获 $broadcast?
【发布时间】:2016-04-30 06:20:03
【问题描述】:

我不知道为什么我的测试不起作用。

it('should be called when "get-tentative-events" is fired', function() {
  spyOn(this.VM, 'addEvents');
  spyOn(this.VM, 'setEventData').and.returnValue(null);

  this.VM.$root.$broadcast('get-tentative-events', []);
  expect(this.VM.addEvents).toBeDefined();
  expect(this.VM.addEvents).toHaveBeenCalled();
});

在调试中,我确认addEvents被调用了。
但是出现了错误single-calendar addEvents should be called when "get-tentative-events" is fired FAILED

仅供参考:

如果我改变了

this.VM.$root.$broadcast('get-tentative-events', []);

this.VM.addEvents([])

我可以通过这个测试。所以我不明白为什么我的测试失败了。

【问题讨论】:

    标签: javascript unit-testing jasmine vue.js


    【解决方案1】:

    在监视 Vue 方法和事件被触发时,我总是遇到麻烦。您可以尝试使用 $nextTick 进行异步测试,以防 Vue 的事件被异步解析,但我建议直接测试您的事件处理程序并相信 Vue 的内部事件处理工作而不是监视这些事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 2015-07-27
      • 2020-03-26
      • 2017-12-12
      相关资源
      最近更新 更多