我正在尝试使用以下代码模拟一个使用 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 它。
任何指针将不胜感激!