【发布时间】:2013-09-10 13:37:29
【问题描述】:
用户资源的声明类似于:
factory('User', function($resource) {
return $resource('/api/user/:userId.json', {}, {
put: {method:'PUT', params: {userId:'@id'}},
});
})
如您所见,PUT 方法的 -default- 参数是资源中的 id 属性。
如果您想测试:
httpBackend.expectPUT('api/user/1.json').respond(200);
userResource.put();
httpBackend.flush();
我在测试中不断失败,因为它正在生成的实际 URL 是:'api/user/.json'。 id 属性未包含在 URL 中。
这是有道理的,因为我没有为模拟对象指定 id 属性,我没有指定,因为我不知道该怎么做。
提前致谢。
【问题讨论】:
-
类似
userResource.put({id:1}); -
@Chandermani 如果
userResource是User的一个实例,那就没有必要了。但是,它确实要求userResource在调用put()时将id属性设置为1。你在测试中设置了吗? -
在相关但单独的说明中,您可能有兴趣阅读我的文章:kirkbushell.me/… 它提供了一种以遵循 RESTful 约定的方式使用 ngResource 的方法:)
标签: angularjs mocking jasmine ngresource