angularjs - 如何在 Jasmine 中模拟 response.headers ('location' )?

标签 angularjs unit-testing jasmine

在 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/

相关文章:

javascript - 使用 angularJS 填充输入和文本区域

c# - 如何断言两个列表包含 NUnit 中具有相同公共(public)属性的元素?

javascript - 如何编写依赖于另一个套件变量的 Jasmine 测试套件? ( Jasmine 1.3)

javascript - AngularJS:通过回车触发功能的推荐方式

javascript - 使用 AJAX 从客户端( Angular )获取数据到 Node.js

c# - 如何对 64 位 .NET 4.7 项目进行单元测试?

ios - 如何学习 iOS 测试

javascript - 在没有服务器的情况下在 Phantom.js 中运行 Jasmine 测试套件

javascript - Jasmine:用事件测试可观察量

javascript - ionic 倒计时应用程序