Given a handler attached to the $routeChangeSuccess event to update the title property on $rootScope:
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
$rootScope.title = 'My title';
});
in unit tests, I expect that performing a route change would update the title property:
it('should set a title on $routeChangeSuccess', function () {
$location.path('/contacts');
$rootScope.$apply();
expect($rootScope.title).toBe('My title');
});
However, the code in the $routeChangeSuccess handler never executes in tests. It executes fine and updates the title when running the application in the browser.
How can I trigger a $routeChangeSuccess event in tests? Is there a better approach to testing any arbitrary code that is attached to $rootScope.$on('$routeChangeSuccess', ...) or other $routeChange events?
Try sending out the event yourself.
$rootScope.$broadcast('$routeChangeSuccess', eventData);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With