7

Here's my run block, where I set a couple of properties on rootScope based on the location path:

angular.module('xyz').run(['$rootScope', '$location', '$route', function($rootScope, $location, $route){
    $rootScope.brand = $location.path().split('/')[1];
    $rootScope.appName = $rootScope.brand.split('.')[2];
});

Here's the unit test that's failing:

beforeEach(module('App'));

beforeEach( inject( function( $controller, _$location_, $rootScope) {
    $location = _$location_;
    $scope = $rootScope.$new();
    AppCtrl = $controller( 'AppCtrl', { $location: $location, $scope: $scope });
}));
it('AppCtrl has been initialized', inject( function() {
    expect( AppCtrl ).toBeTruthy();
}));

Tried something along these lines:

it('should set the default category to match the category_id found in location.hash', function() {
    $browser.setUrl('http://server/#/categories/2');
    $browser.poll();
    scope.$eval();
    $browser.xhr.flush();
    expect(ctrl.selectedCategory).toBe('2'); 
}); 

Didn't help.

1 Answer 1

6

Here is your solution https://groups.google.com/d/msg/angular/F0jFWC4G9hI/FNz3oQu0RhYJ.

Just to let you know, Igor Minar and Vojta Jína are both Angular developers and the latter is one of the main persons behind AngularJs unit testing, so pay attention to them.

So, basically it already uses a mocked version of the $location service while in test and you should be able to perfectly control it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your contribution. Though a quick sumup on SO could help: it makes the quality of your answer more reliable and the content easier to access.
Hi @AsTeR. Thanks for the feedback. But I have summed it up at the last paragraph already (although I just fixed a small issue). Can you please suggest a review on how could it be more complete?
Ohh, think I got you, you mean adding a small example on how to control it? Got you. Will do soon. Thanks @AsTeR.
I was thinking about something like that indeed, sorry for not being more specific and thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.