0
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控制器
angular.module('scrCliApp')
.controller('MainCtrl', function ($scope) {
$scope.$on('$routeUpdate', function(/*scope, next, current*/) {
console.log('i was called'); //never logs
});
});
main.js測試
'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();
});
});
運行測試時
, 「我被稱爲」 永不記錄。
感謝 - 使用EMIT讓我從範圍中刪除我的onRouteUpdateDoSomething方法,但仍對其進行測試。如果事情僅適用於location.search(x,y)&$ apply(),那將會很好。也許它與reloadOnSearch有關:false – 2015-03-25 21:22:04