【问题标题】:In a Jasmine unit test, how do I force a failure callback to trigger that would result from a failed request?在 Jasmine 单元测试中,如何强制触发失败回调以触发失败请求?
【发布时间】:2016-03-15 11:57:12
【问题描述】:

假设我有这个方法(下面是外推的函数):

function doSomething(onSuccess, onFailure){
  var that = this;
  that.$save(function(result){
    if (onSuccess) {
      onSuccess(result);
    }
  }, onFailure);
}

我已经成功测试了 onSuccess 回调会在 that.$save 按预期执行时触发,但我无法找到任何有关如何做到这一点的文档。$save 无法触发 onFailure 回调。

我创建 onFailure 测试的尝试如下:

it ('should trigger the failure callback if necessary', function(){
  var  successCallback= jasmine.createSpy('successCallback');
  var  failureCallback= jasmine.createSpy('failureCallback');

  // Force $save to fail to trigger failure callback
  thing.$save = function(){};
  spyOn(thing, '$save').and.throwError('thing.$save error');

  thing.addNew(successCallback, failureCallback);

  expect(failureCallback).toHaveBeenCalled();
  expect(callback.successCallback).not.toHaveBeenCalled();
});

它仍然会尝试调用成功回调,因为我可以根据此错误判断:

Expected spy failureCallback to have been called.
    at /Users/pathy-path
Error: Unexpected request: POST http://website.com/things/5654d74a9b8d05030083239f
No more request expected

如果我将 httpBackend 设置为期待 POST(它不应该因为它应该失败?至少这看起来合乎逻辑),测试失败,因为错误的回调执行:Expected spy failureCallback to have been called.

作为参考,我的 onSuccess 测试看起来像这样(有些简化):

it ('should trigger the success callback', function(){
  var successCallback = jasmine.createSpy('successCallback');
  path = ENV.apiEndpoint + '/things/' + id;
  // if this is triggered we know it would be saved
  $httpBackend.expect('POST', path).respond(201);

  thing.addNew(successCallback);

  $httpBackend.flush();
  expect(successCallback).toHaveBeenCalled();
});

【问题讨论】:

    标签: javascript unit-testing error-handling jasmine throw


    【解决方案1】:

    您需要让它执行第二个 (onFailure) 回调

    spyOn(that, '$save').and.callFake(function(success, fail) {
        fail();
    });
    

    【讨论】:

    • 这使我的测试通过,但似乎触发 failureCallback 并没有像替换 .$save 方法(这就是我上面所说的那样)。我尝试了一种使用 .$save 但触发失败而不是故意调用它的新方法。
    • @hiMyNameIs-Name 那么究竟你在这里测试什么?从您的问题看来,您正在测试 thing (我假设它是一个控制器),它消耗 that 所以你应该嘲笑 that 接口
    【解决方案2】:

    我尝试了 Phil 的答案(这使测试通过),但觉得它实际上可能不是对 doSomething 函数如何执行的真正测试。为了模仿 API 失败,我最终使用了 $httpBackend 并强制执行 404 响应,以便 Angular 的 .$save 方法仍然执行:

    it ('should trigger the failure callback if necessary', function(){
      var  successCallback= jasmine.createSpy('successCallback');
      var  failureCallback= jasmine.createSpy('failureCallback');
    
      // Force $save to fail to trigger failure callback
      path = ENV.apiEndpoint + '/things/' + thing.id;
      $httpBackend.expect('POST', path).respond(404);
    
      thing.addNew(successCallback, failureCallback);
    
      $httpBackend.flush();
    
      expect(failureCallback).toHaveBeenCalled();
      expect(successCallback).not.toHaveBeenCalled();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2021-12-08
      相关资源
      最近更新 更多