用"解析"单元测试AngularJS路由

mit*_*daa 6 unit-testing angularjs

我试图对我的一条路线进行单元测试,我得到臭名昭着的"错误:意外请求"错误.我的路线采用"resolve"参数,如下所示:

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

其中UserAccounts是一种"资源".我正在测试我的路线如下:

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');
    }));
Run Code Online (Sandbox Code Playgroud)

测试失败了Error: Unexpected request: GET http://myserver/api/Accounts/123/UserAccounts.这是我的决心所要求的.有什么想法正确的方法来测试这样的路线?

mit*_*daa 2

哇!这是一件愚蠢的事。我在资源中定义了完整的 URL

http://myserver/api/Accounts/:accountId/UserAccounts/:userId
Run Code Online (Sandbox Code Playgroud)

我设置了$httpBackend检查相对路径的期望。

api/Accounts/123/UserAccounts
Run Code Online (Sandbox Code Playgroud)