【发布时间】:2015-05-29 16:32:32
【问题描述】:
在我的控制器中,在实例化时,我正在调用一个调用范围方法的异步方法:
app.controller 'MyCtrl', ($scope,mySvc) ->
## do some initial stuff
mySvc.asyncMethod
.then (an_array) ->
val = $scope.myScopedMethod
我的测试是这样的:
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})
# this assertion works
it "should call asyncMethod", () ->
controller = createController()
expect(mySvc.asyncMethod).toHaveBeenCalled()
# this also works
it "should define myScopedMethod", () ->
controller = createController()
expect(angular.isFunction($scope.myScopedMethod)).toBe true
# this fails with 'Error: Expected a spy, but got Function.'
it "should call $scope.myScopedMethod", () ->
controller = createController()
$scope.$digest()
expect($scope.myScopedMethod).toHaveBeenCalled()
无论我是否调用$digest(),都会遇到同样的错误。我期待$digest() 解决asyncMethod 以便它调用myScopedMethod,但有些不对劲。
【问题讨论】:
标签: angularjs unit-testing asynchronous jasmine