1

My test has:

it("should clear the search field when the URL changes", function() {
  createController();
  $scope.init();
  $scope.searchTerm = 'some term';

  $location.path('/source');
  $rootScope.$apply();

  expect($scope.searchTerm).toBe('');
});

My controller is:

angularMoonApp.controller('SearchController', ['$scope', '$location', function ($scope, $location) {
  $scope.init = function() {
  $scope.$on('$routeChangeStart', function(next, current) {
    $scope.searchTerm = '';
  });

  }

  $scope.init();
}]);

Seems simple enough! So why won't that trigger when I Change the location in the test?

Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • Maybe this will help: http://stackoverflow.com/questions/15990102/angularjs-route-unit-testing – aet May 07 '14 at 18:44

1 Answers1

3

You need to inject $route, since $routeChangeStart is an event triggered by $route.

angularMoonApp.controller('SearchController', ['$scope', '$location', '$route', function ($scope, $location, $route) {

Without knowing your use case, if you just need to detect that the url changed, you can listen for $locationChangeStart instead. $locationChangeStart is fired from $location, so you would not need to inject any new dependencies.

Scott Rice
  • 2,430
  • 22
  • 18