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.confirmModalconsole.log できるので、このコンテキストには間違いなく存在します。

任意のポインタをいただければ幸いです!

4

1 に答える 1