【问题标题】:Karma Angular.js unit test expectPUTKarma Angular.js 单元测试 expectPUT
【发布时间】:2015-07-10 09:05:52
【问题描述】:

我在使用 karma 进行单元测试时遇到了问题。

  $httpBackend.expectPUT(envConfig.restUrl + 'customers/1').respond(200);
  it('should update"customer details" model', function() {
      expect(scope.customerDetails).toBeUndefined();
      $httpBackend.flush();

以及回应:

 Error: Unsatisfied requests: PUT undefinedcustomers/1
 at Function.$httpBackend.verifyNoOutstandingExpectation 

【问题讨论】:

  • envConfig.restUrl 是否已定义?
  • 没有问题 - 您期待一些 PUT 调用,但从未发生 - $httpBackend.flush() 尝试刷新所有请求,但没有待处理的请求。
  • @KrzysztofSafjanowski 查看请求的 url => PUT undefinedcustomers/1
  • @ashish 看看it 部分 - 没有什么可以执行 PUT 查询
  • 是的,将其更改为whenPUT 可能会修复它。 :)

标签: javascript angularjs unit-testing karma-jasmine


【解决方案1】:

您的envConfig.restUrl 未定义。

根据@KrzysztofSafjanowski 的评论,您也没有调用可能执行PUT 请求或手动执行此操作的函数。将其更改为 whenPUT 可能会解决此问题:

$httpBackend.whenPUT(envConfig.restUrl + 'customers/1').respond(200);
  it('should update"customer details" model', function() {
      expect(scope.customerDetails).toBeUndefined();
      $httpBackend.flush(); 

【讨论】:

  • 已定义。 expectGET 测试成功
  • @kret 不 - 它没有定义 - 显示更多/完整的测试代码,所以我们可以尝试找到错误源
  • @kret 我不知道——这取决于你想要做什么。请举一些像stackoverflow.com/a/31316620/2708895 这样的例子,我们会尝试找出问题所在
猜你喜欢
  • 1970-01-01
  • 2018-12-19
  • 2018-02-19
  • 2017-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
相关资源
最近更新 更多