【问题标题】:Jasmine SpyOn Window Function in AngularJS ApplicationAngularJS 应用程序中的 Jasmine SpyOn 窗口函数
【发布时间】:2015-07-04 01:53:16
【问题描述】:

我已经在路由更改时创建了一个函数:

window.intercom = function(action, msg) {
  return window.Intercom(action, msg);
};

我试图窥探这个功能,但不明白如何。我遵循了this SO post.中的建议

如果我使用这个:

beforeEach(inject(function($compile, $rootScope, $q, _$location_, $routeParams) {
  element = angular.element("<add-box></add-box>");
  $compile(element)($rootScope);
}));

it("should cancel adding a box!", function() {
  spyOn(window, 'intercom')
});

我收到一个错误:

intercom() method does not exist

所以我尝试了这个:

it("should cancel adding a box!", function() {
  var intercom = jasmine.createSpy();
});

上面写着:

TypeError: 'undefined' is not a function (evaluating 'window.intercom('hide')')

我怎样才能让它工作?我确定这很简单,我只是 Jasmine 的新手。

【问题讨论】:

  • 请提供更多上下文。发送实际的测试代码。
  • @RobertMoskal 目前测试中没有太多其他内容,因为我只是想让它们运行。进行了一些更改。

标签: angularjs jasmine intercom.js


【解决方案1】:

如何在 beforeEach 或更好的 before 函数中执行以下操作:

window.intercom = jasmine.createSpy();

这会给你一个窗口上的对讲功能。

【讨论】:

  • 谢谢。我以前尝试过,但放入 beforeeach 块似乎有效。
猜你喜欢
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多