我试图对我的一条路线进行单元测试,我得到臭名昭着的“错误:意外请求”错误.我的路线采用“resolve”参数,如下所示:
原文链接:https://www.f2er.com/angularjs/140325.htmlwhen('/Users',{ templateUrl: 'app/users/user-list.tpl.html',controller: 'UserListCtrl',resolve: { userAccounts: ['UserAccounts',function(UserAccounts) { return UserAccounts.query({ id: 123 }); }] } })
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'); }));
测试失败,错误:意外请求:GET http:// myserver / api / Accounts / 123 / UserAccounts.这是我的决心所要求的.有什么想法正确的方法来测试这样的路线?