2015-03-25 48 views
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(); 
    }); 
}); 
運行測試時

, 「我被稱爲」 永不記錄。

回答

1

也許你應該剛剛播出自己的情況,然後測試該怎麼用它做:

it('do call', function() { 

    scope.$emit('$routeUpdate', [1,2,3]); 
    scope.$apply(); 

    expect.... 
}); 
+0

感謝 - 使用EMIT讓我從範圍中刪除我的onRouteUpdateDoSomething方法,但仍對其進行測試。如果事情僅適用於location.search(x,y)&$ apply(),那將會很好。也許它與reloadOnSearch有關:false – 2015-03-25 21:22:04