是否可以使用jasmine模拟ajax请求来模拟网络故障?

时间:2016-03-14 16:01:44

标签: javascript jasmine jasmine-ajax

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

在Jasmine中,我可以使用request.respondWith({status:400, etc})来模拟失败,但我无法弄清楚如何模拟未完成调用的更基本错误。

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

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

1 个答案:

答案 0 :(得分:0)

这是一个有趣的问题。虽然我可能不会为你提供一个直截了当的答案,但我想抓住它。

下面的代码说明了三个这样的例子(相反测试)。在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是从完成调用的errorFunction 如果readySatate === 4 && status === 0以及所有其他 error / fail回调
  • makeAPICall返回jqXHR&{使用了done个回调。
  • fail是一个实用程序函数,它生成一个巨大的有效负载:用于test1
  • 第二个测试是关于使用空getData进行虚拟ajax调用以模拟url
  • 第三个测试模拟了通常的错误请求url模式测试。
  • 对于所有3个场景,我使用了由readyState->4 & status->0
  • 创建的jqXHR对象
  • 设置$.ajax({some params & async false})帮助我生成一个虚拟对象并通过async: false
  • 将其传递给函数
相关问题