从我的控制器中,在实例化时,我调用一个异步方法,该方法调用一个作用域方法:
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,但有些地方不太对劲。
发布于 2015-06-01 23:17:42
scope不是间谍
spy是使用
var spy = spyOn($scope, 'myScopedMethod')然后
expect($scope.myScopedMethod)https://stackoverflow.com/questions/30534679
复制相似问题