在 Angular Controller 中,我有一些代码,例如:
$http.save('api/purchases').$promise.then(function(response) {
var location = response.headers('location'); // to retrieve the location in response header
})
在 jasmine 的单元测试中,
it('..', function(){
$httpBackend.expectPost('api/purchase').respond(201, {}, {location: 'xxx'});
// when run the test, it reports an error with 'undefined is not a constructor' (evaluting response.headers('location'))
})
我如何测试 response.headers('location')?
最佳答案
Angular docs似乎表明在给定 headers
时响应需要 statusText
(因为它不是可选的,并且在传入 headers
时是必需的)
function([status,] data[, headers, statusText])
你应该能够像这样为 201 传递一些文本
$httpBackend.expectPost('api/purchase').respond(201, {}, {location: 'xxx'}, 'Created');
关于angularjs - 如何在 Jasmine 中模拟 response.headers ('location' )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41397813/