【问题标题】:Is it possible to mock an ajax request with jasmine simulating a network failure?是否可以使用 jasmine 模拟网络故障来模拟 ajax 请求?
【发布时间】:2016-03-14 16:01:44
【问题描述】:

我有一个网络应用程序需要检查用户是否连接到互联网。在实现中,如果对已知端点的 ajax ping 成功,则 check() 函数承诺为 true,如果 ajax 调用以任何方式失败,则返回 false。

在 Jasmine 中,我可以使用 request.respondWith({status:400, etc}) 来模拟失败,但我不知道如何模拟根本没有调用的更根本错误。

实际上,当调用甚至无法进行时,浏览器似乎会“返回”状态码 0 和 readyState 4。

我应该如何在 Jasmine 测试中解决这个问题?

【问题讨论】:

  • 您可以使用window.navigator.onLine 来检查您是否仍有网络连接。
  • 你不能 ping 一个不存在的 URL 吗?这将始终以失败状态响应

标签: javascript jasmine jasmine-ajax


【解决方案1】:

这是一个有趣的问题。虽然我可能不会为您提供直接的答案,但我想试一试。

下面的代码说明了三个这样的例子(而不是测试)。见action

  var errorFunction = function(jqXHR, status, error) {  
    console.log('This is errorFunction')  
  }

  var makeAPICall = function(url) {
    return $.ajax({
      url: url,
      fail: errorFunction,
      error: errorFunction
    });
  }

  var testFunction = function() {
    makeAPICall().done(function(data, status, xh) {
      if (xh.status === 0 && xh.readyState == 4) {
        errorFunction();
        return;
      }
      console.log("this is successFunction");
    }).fail(errorFunction);
  }

  var getData = function() {
    var str = 'ABCDEFGHIJ'
    var returnStr = '';
    for (var i = 0; i < 3000; i++) {
      returnStr += str;
    }
    return returnStr;
  }

  describe('test ajax errors', function() {
    it('tests a failed call due to huge payload ', function() {
      var xhrObj = $.ajax({
        url: 'https://jsonplaceholder.typicode.com/posts?userId=' + getData(),
        async: false
      });
      console.log('Below is the xhr object that is breing returned via spy');
      console.log(xhrObj);    
      spyOn(window, 'makeAPICall').and.returnValue(xhrObj);
      spyOn(window, 'errorFunction').and.callThrough();    
      testFunction();
      expect(window.errorFunction).toHaveBeenCalled();
    });

    it('tests a failed call due to some n/w error scenario (readyState->4, status->0)', function() {
      var xhrObj = $.ajax({
        url: '',      
        async: false
      });
      xhrObj.status = 0;
      xhrObj.statusText = 'error';
      console.log('Below is the xhr object that is breing returned via spy');
      console.log(xhrObj);    
      spyOn(window, 'makeAPICall').and.returnValue(xhrObj);
      spyOn(window, 'errorFunction').and.callThrough();    
      testFunction();
      expect(window.errorFunction).toHaveBeenCalled();
    });

    it('tests a failed call (bad url pattern)', function() {
      var xhrObj = $.ajax({
        url: 'https://jsonplaceholder.typicode.com/postssss/1',
        async: false
      });
      console.log('Below is the xhr object that is breing returned via spy');
      console.log(xhrObj);    
      spyOn(window, 'makeAPICall').and.returnValue(xhrObj);
      spyOn(window, 'errorFunction').and.callThrough();    
      testFunction();
      expect(window.errorFunction).toHaveBeenCalled();
    });


  });

注意事项:

  • errorFunction 是从 done 调用的 errorFunction 如果readySatate === 4 &amp;&amp; status === 0 以及所有其他 error/fail 回调
  • makeAPICall 返回使用了 donefail 回调的 jqXHR
  • getData 是一个实用函数,可生成巨大的有效载荷:在 test1 中使用
  • 第二个测试是关于用空url进行虚拟ajax调用以模拟readyState-&gt;4 &amp; status-&gt;0
  • 第三个测试模拟了通常的错误请求 url 模式测试。
  • 对于所有 3 个场景,我使用了由 $.ajax({some params &amp; async false}) 创建的 jqXHR 对象
  • 设置async: false 帮助我生成一个虚拟对象并通过spy 将其传递给函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多