有人可以建议我如何模仿$resource
对象
我已经通过互联网进行了搜索,但我的所有尝试都是通过KARMA测试完成的。 我不需要它。
我的想法是拥有假对象,因此我可以在我的应用中的$resource
实现之间切换。
感谢。
答案 0 :(得分:2)
您可以使用$provide执行此操作。
angular.module(“MyApp”,[])
.config([“$provide”,function($provide){
$provide.decorator(“$resource”,function($delegate, myReplacementResource){
//$delegate is the original $resource, if you just want to modify it
//either inject a replacement resource that you have already registered
//as a factory (recommended). Or make it here.
return myReplacementResource;
});
}])
答案 1 :(得分:1)
dskh介绍了一种方法。这是您可能会发现更容易的另一种方式......虽然它用于单元测试,但您也可以在应用中使用angular-mocks.js:
app.run(function($httpBackend) {
$httpBackend.whenPOST('/string/match/url').respond(function (method, url, data) {
return [{some:data}];
});
$httpBackend.whenGET(/regexpmatch/).respond(function (method, url, data) {
return {some:{other:data}};
});
// pass through other stuff
$httpBackend.whenPOST(/.*/).passThrough();
$httpBackend.whenGET(/.*/).passThrough();
$httpBackend.whenDELETE(/.*/).passThrough();
$httpBackend.whenJSONP(/.*/).passThrough();
$httpBackend.whenPUT(/.*/).passThrough();
});
答案 2 :(得分:1)