我的控制器是:
webApp.controller 'AboutUsController', [
'$scope'
'$rootScope'
($scope, $rootScope) ->
$scope.init = ->
console.log 'About Us Page'
$scope.init()
]
在我的业力测试中,我有:
describe 'About Us Controller', ->
$scope = null
$rootScope = null
controller = null
beforeEach ->
module 'webApp'
inject ($injector) ->
$rootScope = $injector.get '$rootScope'
$scope = $rootScope.$new()
$controller = $injector.get '$controller'
controller = $controller 'AboutUsController',
'$scope': $scope
'$rootScope': $rootScope
spyOn($scope, 'init').andCallThrough()
it 'should should have an About Us Controller', ->
expect(controller).not.toBe null
expect(controller).not.toBe undefined
it 'should call the init function', ->
expect($scope.init).toHaveBeenCalled()
非常简单,对吧?但我得到PhantomJS 1.9.7 (Mac OS X) About Us Controller should call the init function FAILED
Expected spy init to have been called.
为什么会这样?
答案 0 :(得分:0)
而不是监视init(),而是编写一些测试来验证init()执行的任务是否已经发生。另一个想法是将init()执行的工作移动到服务中。这样,您可以在创建控制器之前实例化服务并监视它。