2017-01-12 11 views
0

コントローラでコールバックが呼び出されたかどうかをテストしようとしています。

コントローラ

(function() { 
    'use strict'; 

    angular 
     .module('GeoDashboard') 
     .controller('CiudadCtrl', CiudadCtrl); 

    CiudadCtrl.$inject = ['$scope', '$interval', '$rootScope','Ciudad']; 

    function CiudadCtrl($scope, $interval, $rootScope, Ciudad) { 

     $scope.refreshCiudad = refreshCiudad; 
     $scope.refreshClima = refreshClima; 

     var removeListenerRefreshCiudad = $scope.$on('refreshCiudad', refreshCiudad); 
     var cancelIntervalIdClima = $interval(refreshClima, 600000); 

     function refreshCiudad() { 
      //... 
     } 
    } 

})(); 

テスト

beforeEach(inject(eachSpec)); 

function eachSpec($rootScope, $controller, $httpBackend, $interval, _Ciudad_){ 

    rootScope = $rootScope; 
    scope = $rootScope.$new(); 
    httpBackend = $httpBackend; 
    interval = sinon.spy($interval); 

    CodificacionGeograficaService = _CodificacionGeografica_; 
    CiudadService = _Ciudad_; 

    CiudadController = $controller('CiudadCtrl', { 
     $scope : scope, 
     $interval: interval, 
     Ciudad: CiudadService 
    }); 
} 

it('3. Debería llamar a "refreshCiudad" al escuchar el evento "refreshCiudad"', spec3); 

function spec3(){ 

    sinon.spy(scope, 'refreshCiudad'); 

    rootScope.$broadcast('refreshCiudad'); 

    expect(scope.refreshCiudad).toHaveBeenCalled(); 
    scope.refreshCiudad.restore(); 
} 

しかし、私は$放送イベントを放出しようとすると、コールバックが呼び出されたされていません。

何が間違っているのですか?

+0

のような別の関数に$scope.$onためのコールバック関数をラップした$ダイジェスト();あなたの期待声明の直前ですか? –

+0

はい、私は試して、働いていません:( –

答えて

0

最後にこれは機能していますが、理由はわかりません。あなたはスコープを試してみました ソリューションは、この

function CiudadCtrl($scope, $interval, $rootScope, Ciudad) { 

     $scope.refreshCiudad = refreshCiudad; 

     //instead this 
     //var removeListenerRefreshCiudad = $scope.$on('refreshCiudad', refreshCiudad); 
     //I set the callback like this 
     var removeListenerRefreshCiudad = $scope.$on('refreshCiudad', function(){ 
      $scope.refreshCiudad(); //And this work! 
     }); 

     function refreshCiudad() { 
      //... 
     } 
    } 
関連する問題