Javascript 在AngularJS中创建新资源后无法获取新对象属性
我试图在AngularJS中创建Person类型的新资源。创建资源后,我希望获得新的资源id(由服务器返回) 我在业力中得到了“预期未定义为32” 我认为我的代码片段与信用卡代码片段末尾提供的代码片段非常接近Javascript 在AngularJS中创建新资源后无法获取新对象属性,javascript,angularjs,jasmine,angularjs-resource,Javascript,Angularjs,Jasmine,Angularjs Resource,我试图在AngularJS中创建Person类型的新资源。创建资源后,我希望获得新的资源id(由服务器返回) 我在业力中得到了“预期未定义为32” 我认为我的代码片段与信用卡代码片段末尾提供的代码片段非常接近 你知道我做错了什么吗 您需要调用$httpBackend.flush()来刷新请求 试试这个: it('should get the new Person id after add', inject(function($httpBackend, Person) { $htt
你知道我做错了什么吗 您需要调用
$httpBackend.flush()
来刷新请求
试试这个:
it('should get the new Person id after add', inject(function($httpBackend, Person) {
$httpBackend.whenPOST('http://api.example.com/Persons').respond({ "id" : 32});
var newPerson = new Person();
newPerson.$save();
$httpBackend.flush();
expect(newPerson.id).toEquals(32);
}));
我得到:“超时:等待新数据填充500毫秒后超时”我明白了,您没有调用
$httpBackend.flush()
来刷新请求。编辑我的答案。
it('should get the new Person id after add', inject(function($httpBackend, Person) {
$httpBackend.whenPOST('http://api.example.com/Persons').respond({ "id" : 32});
var newPerson = new Person();
newPerson.$save();
$httpBackend.flush();
expect(newPerson.id).toEquals(32);
}));