angularjs - Jasmine not recognizing spied on method called from asynchronous function resolution -


from controller, upon instantiation, calling asynchronous method calls scope method:

app.controller 'myctrl', ($scope,mysvc) ->   ## initial stuff   mysvc.asyncmethod   .then (an_array) ->     val = $scope.myscopedmethod 

my test so:

describe "my tests", () ->   $controller = undefined   $scope = undefined   $q = undefined   createcontroller = undefined   mysvc = undefined    beforeeach inject ($controller, $rootscope, $q, _mysvc_) ->     $scope = $rootscope.$new()     mysvc = _mysvc_     deferred = $q.defer()     deferred.resolve []     spyon(mysvc,'asyncmethod').and.returnvalue deferred.promise     spyon($scope, 'myscopedmethod').and.callthrough()     createcontroller = () ->       return $controller('myctrl', {$scope: $scope, mysvc: mysvc})    # assertion works   "should call asyncmethod", () ->     controller = createcontroller()     expect(mysvc.asyncmethod).tohavebeencalled()    # works   "should define myscopedmethod", () ->     controller = createcontroller()     expect(angular.isfunction($scope.myscopedmethod)).tobe true    # fails 'error: expected spy, got function.'   "should call $scope.myscopedmethod", () ->     controller = createcontroller()     $scope.$digest()     expect($scope.myscopedmethod).tohavebeencalled() 

i same error whether call $digest() or not. expecting $digest() resolve asyncmethod calls myscopedmethod, not right.

it looks $scope.myscopedmethod not yet exist when try create spy it.

you need create spy after function created. created part of controller initialization, maybe this:

beforeeach inject ($controller, $rootscope, $q, _mysvc_) ->     $scope = $rootscope.$new()     mysvc = _mysvc_     deferred = $q.defer()     deferred.resolve []     spyon(mysvc,'asyncmethod').and.returnvalue deferred.promise     createcontroller = () ->         ctrl= $controller('myctrl', {$scope: $scope, mysvc: mysvc})         spyon($scope, 'myscopedmethod').and.callthrough()         return ctrl 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -