1

User 资源的声明类似于:

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 属性,我没有指定,因为我不知道该怎么做。

提前致谢。

4

1 回答 1

3

路径应以“/”开头,您需要传入一个 ID 以使路径与代码中生成的内容匹配。URL 匹配是字符串匹配,因此您需要保证您希望命中的 URL 与生成的 URL 完全相同。

httpBackend.expectPUT('/api/user/1.json').respond(200);
userResource.put({id:1});
httpBackend.flush();
于 2013-09-06T14:20:37.457 回答