jeudi 29 janvier 2015

Testing a $watch on $location.absUrl() in angular not working


I'm trying to test a watch I have on $location.absUrl() like the following:



scope.$watch((function() {
return $location.absUrl();
}), function() {
sync();
});


and my test looks like:



it('it should be able to set the path and update state through location search', inject(function($location, $rootScope, StateManager){
var stateManager = new StateManager('fakeModule', $rootScope.$new(), {defaults: {test: true}})
$location.path('/test');
$location.search('state', JSON.stringify({ globals: { test: true, account: 5 }}));

$rootScope.$apply();

expect($location.url()).to.equal('/test?state=%7B%22globals%22:%7B%22test%22:true,%22account%22:5%7D%7D');
}));


This test is kind of pointless but I'm slowly working my way up to testing the result of the sync function but I can't even run the above test because I get the error:



Warning: FAIL: main - State manager module - 'it should be able to set the path and update state through location search' (20ms)
TypeError: 'undefined' is not an object (evaluating '$location.absUrl')


The scope.watch is set in the StateManager so it's trying to call it when that gets initialized.





Aucun commentaire:

Enregistrer un commentaire