【发布时间】:2017-01-12 16:29:38
【问题描述】:
我一直在尝试测试是否在我的控制器上调用了回调。
控制器
(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();
}
但是当我尝试发出 $broadcast 事件时,回调并没有被调用。
我做错了什么?
【问题讨论】:
-
你试过 scope.$digest();就在您的期望声明之前?
-
是的,我已经尝试过但没有工作:(
标签: javascript angularjs unit-testing jasmine sinon