mardi 24 mars 2015

$routeUpdate not broadcast in angular karma unit test?


app.js



'use strict';

angular
.module('scrCliApp', [
'ngRoute',
'ui.bootstrap'
])
.config(function ($routeProvider) {
$routeProvider
.when('/search', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
reloadOnSearch: false
})
.otherwise({
redirectTo: '/search'
});
});


main.js controller



angular.module('scrCliApp')
.controller('MainCtrl', function ($scope) {
$scope.$on('$routeUpdate', function(/*scope, next, current*/) {
console.log('i was called'); //never logs
});
});


main.js test



'use strict';

describe('Controller: MainCtrl', function () {

// load the controller's module
beforeEach(module('scrCliApp'));

var MainCtrl,
scope,
rootScope,
location;

// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, $location) {
rootScope = $rootScope;
location = $location;
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
}));

it('do call', function () {
location.search('q', 'b');
scope.$apply();
});
});


when running the test, "i was called" never logs.





Aucun commentaire:

Enregistrer un commentaire