2015-07-21 71 views
0

單元測試控制器我試圖單元測試使用$ ionicView.enter這樣

myControllers.controller('MyCtrl', [ 
'$scope', 
function($scope) { 
    'use strict'; 

    $scope.$on('$ionicView.enter', function(){ 
    $scope.myValue = 1; 
    }); 

}]); 

在我的單元測試控制器我可以窺視事件,並檢查它已被調用,但它不會進入函數並設置$ scope.myValue。這是我的測試:

it('should do stuff', function() { 
    spyOn(scope, '$on').and.callThrough(); 
    scope.$on('$ionicView.enter'); 

    // this passes 
    expect(scope.$on).toHaveBeenCalledWith('$ionicView.enter') 
    // this fails - scope.myValue is undefined 
    expect(scope.myValue).toEqual(1); 
}); 

我以爲callThrough會調用我正在監視的功能,但似乎並沒有在這裏的代碼。

任何幫助表示讚賞。

回答

1

答案是在我的控制器在測試中設置後觸發ionicView輸入事件,而不是試圖窺探它。

beforeEach(inject(function($controller) { 
    scope = $rootScope.$new(); 

    $controller('MyCtrl', { 
    $scope: scope 
    }); 

    scope.$emit('$ionicView.enter'); 

    scope.$digest(); 
})); 
+0

你能顯示所有的代碼嗎? :) – jdnichollsc