似乎
this是工作解决方案,显示如何使用$httpBacked
http://jsfiddle.net/EgMpe/8/
但对于我的情况:
路线
app.config(['$routeProvider',function($routeProvider) { $routeProvider. when('/',{templateUrl: 'partials/user-list.html'}).
…
假服务:
app.run(function($httpBackend) { var users = [{"id":1,"name":"bob","email":"bob@bobs.com"},{"id":2,"name":"bob2","email":"bob2@bobs.com"}] $httpBackend.whenGET('/rest/users').respond(function(method,url,data) { console.log("Getting users"); return [200,users,{}]; }); });
..
真实服务:
services.factory('Users',function($resource){ return $resource('/rest/users',{},{ get: {method: 'GET',isArray:true} }); });
当我转到我的“/”路由,将我重定向到user-list.html页面时,我有错误:
Error: Unexpected request: GET partials/user-list.html No more request
expected
at $httpBackend …/mysite/public/angular/libs/angular-1.2.0/angular-mocks.js:1060:9)
问题1:httpBackend是否阻止其他http请求?
我试图使用passThrough方法让http命中真正的服务器端:
$httpBackend.whenGET(/^\/mysite\//).passThrough();
但这没有帮助.
使用$httpBackend,您必须提前指定要执行的所有请求.也许这个使用AngularJS掌握Web应用程序开发的这段简短的摘录将会说明为什么:
The verifyNoOutstandingExpectation method verifies that all the expected calls were made ($http methods invoked and responses flushed),while the verifyNoOutstandingRequest call makes sure that code under test didn’t trigger any unexpected XHR calls. Using those two methods we can make sure that the code under the test invokes all the expected methods and only the expected ones.