【发布时间】:2015-12-04 14:16:36
【问题描述】:
我有一个 $resource 用于处理实体的 crud http 请求。它包含在我的自定义服务中,目前没有添加任何功能,所以别介意 - 它只是旧的普通 $resource:
app.factory('coursesService', ['$resource', coursesService]);
function coursesService($resource) {
return $resource('/api/courses/:id', {
id: '@id'
}, {
update: {
method: 'PUT'
}
});
}
所以,我正在尝试删除控制器中的一个实例(存储在控制器对象的courseToDelete 属性中):
this.courseToDelete.$remove()
我正在使用来自 ngMockE2E 模块的 $httpBackend 服务来模拟后端:
$httpBackend.whenDELETE(/api\/courses\/(.+)/, undefined, undefined, ['id'])
.respond(function(method, url, data, headers, params) {
clientStorage.deleteCourse(params.id);
return [200, {}];
});
所以当 $resource 发送 DELETE http 请求时,我希望能够从我的本地存储中删除它(因为我没有后端,数据存储在本地存储中作为序列化 JSON 字符串)
我要删除的对象:
{
id: 1,
name: 'Course 1',
duration: 380,
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' +
' Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.' +
' Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur' +
' sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
created: new Date(2011,10,30),
authors: [
'Пушкин', 'Лермонтов'
]
}
从docs 我预计我的params 参数上会有id 属性,但是当函数执行时params 是未定义的,尽管所有其他参数看起来都很好。我在这里做错了什么?
【问题讨论】:
-
courseToDelete对象长什么样?它有 Id 属性吗? -
@Gustav,我将此信息添加到问题中
-
我更喜欢 $httpBackend.expectDELETE 到 $httpBackend.whenDELETE ... 期望您在单元测试中使用 $httpBackend.flush() 在您希望进行调用时。这确保了调用,否则测试失败。如果从未进行过调用,whenDELETE 不会导致测试失败,因此您无法确定是否曾在单元测试中进行过调用。
标签: javascript angularjs single-page-application angularjs-e2e httpbackend