mithun_daa
mithun_daa

Reputation: 4384

Unit Testing AngularJS route with "resolve"

I am trying to unit test one of my routes and I get the infamous "Error: Unexpected request" error. My route takes in a "resolve" parameter and looks like this:

when('/Users',
                {
                    templateUrl: 'app/users/user-list.tpl.html',
                    controller: 'UserListCtrl',
                    resolve: {
                        userAccounts: ['UserAccounts', function(UserAccounts) {
                            return UserAccounts.query({ id: 123 });
                        }]
                    }
                })

where UserAccounts is a "resource". I am testing my route as follows:

beforeEach(inject(function (_$route_, _$location_, _$rootScope_, _$httpBackend_) {
        $route = _$route_;
        $location = _$location_;
        $rootScope = _$rootScope_;
        $httpBackend = _$httpBackend_;

        $httpBackend.when('GET', 'api/Accounts/123/UserAccounts').respond([]);
        $httpBackend.when('GET', 'app/users/user-list.tpl.html').respond({});
    }));

it('/Users should get user-list template and use UserListCtrl', inject(function($controller) {
            $httpBackend.expectGET('app/users/user-list.tpl.html');
            $httpBackend.expectGET('api/Accounts/123/UserAccounts');

            expect($route.current).toBeUndefined();
            $location.path('/Users');
            $rootScope.$digest();

            expect($route.current.loadedTemplateUrl).toBe('app/users/user-list.tpl.html');
            expect($route.current.controller).toBe('UserListCtrl');
    }));

And the test fails with Error: Unexpected request: GET http://myserver/api/Accounts/123/UserAccounts. This is the get that my resolve is calling. Any ideas what the right way is to test such a route?

Upvotes: 6

Views: 3880

Answers (4)

pansay
pansay

Reputation: 687

To unit test resolved values of a route:

var resolvedUserAccounts = $injector.invoke($route.current.$$route.resolve.userAccounts);

Upvotes: 0

mithun_daa
mithun_daa

Reputation: 4384

Wow! this was a silly one. I had the complete URL defined in my Resource

http://myserver/api/Accounts/:accountId/UserAccounts/:userId

And I was setting up expectation on my $httpBackend to check for relative paths.

api/Accounts/123/UserAccounts

Upvotes: 2

digitil
digitil

Reputation: 919

You can try mocking the service

beforeEach(module(function($provide) {
    $provide.value('UserAccounts', {
        query: angular.noop
    });
}));

If you care whether the service method has been called, you can create a spy.

beforeEach(function() {
    spyOn(UserAccounts, 'query');
});

Upvotes: 3

Coding Smackdown
Coding Smackdown

Reputation: 1203

Two things look strange about your test code.

First, your beforeEach method has the GET requests duplicated with those being called in the actual test. Try removing them.

Second, it looks as if you are missing a call to $httpBacken.flush() before your final assertions.

Hopefully this helps.

Upvotes: 3

Related Questions