我有一個方法定義在控制器上,我試圖測試它是否被調用。我能夠測試該方法是否已定義,但是當我嘗試並測試它是否已被調用時,它會抱怨它預計該間諜已被調用。茉莉花不報告角度控制器功能已被稱爲
這裏是我的控制器:
myControllers.controller 'MyCtrl', ($scope) ->
this.init =() -> doStuff()
this.init()
這裏是我的測試套件:
describe "testing myCtrl",() ->
beforeEach "myControllers"
$controller = {}
$scope = {}
beforeEach inject (_$controller_,$rootScope) ->
$controller = _$controller_
$scope = $rootScope.$new()
describe "MyCtrl",() ->
beforeEach() ->
MyCtrl=$controller('MyCtrl',{$scope:$scope})
it "should be defined",() -> # passes
expect(MyCtrl).toBeDefined()
it "should define #init",() -> # passes
expect(angular.isFunction(MyCtrl.init)).toBe true
it "should call #init",() -> # fails
spyOn MyCtrl, 'init'
expect(MyCtrl.init).toHaveBeenCalled()
在錯誤的最後斷言結果:預計間諜init進程已被調用。
我應該如何構造它?我應該在斷言中實例化控制器還是在beforeEach中創建間諜? –