1

我正在尝试使用以下代码模拟一个使用 spyOn 返回承诺的函数:

  beforeEach(inject(function($controller, $rootScope, $q) {
    scope = $rootScope.$new();
    q = $q;

    ctrl = $controller('BillingCtrl', {
      $scope: scope,
      $q: q
    });
  }));

  it('should have overlay off when cancel modal is shown', 
    function() {

      var deferred = q.defer();
      deferred.resolve();

      spyOn(scope, 'confirmModal').andReturn(deferred.promise); 

      scope.cancelSubscription(""); //scope.confirmModal is called within here.
      expect(scope.overlayOn).to.equal(true);
  });

这会引发此错误:

Chrome 37.0.2062 (Mac OS X 10.9.2) Unit: BillingController "before each" hook: workFn FAILED
    TypeError: Cannot read property 'spyOn' of null

scope.confirmModal肯定存在于这种情况下,因为我能够 console.log 它。

任何指针将不胜感激!

4

1 回答 1

0

我想到了。

Mocha 与 Jasmine 发生冲突(我都在使用它们)。

当我删除 Mocha 时,spyOn 功能可以正常工作。

于 2014-10-07T10:48:10.040 回答