Reputation: 1461
I'm trying to mock a route change success in order to write a test. A url i.e. 'example/:id' has been registered using routeProvider.
$scope.$on('$routeChangeSuccess', function(angularEvent, current, previous) {
var super_important_id = current.pathParams.id
// Do something with super_important_id
})
I've tried
$location.path('/example/0')
$rootScope.$apply();
$rootScope.$broadcast('$routeChangeSuccess')
But it always throws this error when I run the test
TypeError: 'undefined' is not an object (evaluating 'current.pathParams')
How can I successfully mock the route change success event and make it fill in pathParams?
Upvotes: 0
Views: 872
Reputation: 40337
If you're manually broadcasting the $routeChangeSuccess
event, then you need to provide the arguments for it. Something like this:
var objToMockCurrentRoute = {
pathParams: {
id: "MY_ID"
//any other needed properties
}
//any other needed properties
}
$rootScope.$broadcast('$routeChangeSuccess', objToMockCurrentRoute);
Without doing that, current
is just undefined
, which explains the error you're seeing.
Also, doing it this way means you don't need to call $location.path
.
Upvotes: 2