AngularJS 应用程序中的 Jasmine SpyOn Window 函数

Jasmine SpyOn Window Function in AngularJS Application

我创建了一个在路由更改时这样的函数:

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 的新手。

如何在 beforeEach 或更好的 before 函数中做这样的事情:

window.intercom = jasmine.createSpy();

这将使您在 window 上获得对讲功能。