首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用jasmine-ajax验证是否调用了send方法?

如何使用jasmine-ajax验证是否调用了send方法?
EN

Stack Overflow用户
提问于 2019-04-11 03:57:56
回答 1查看 73关注 0票数 2

如果我从来没有调用过jasmine-ajax方法,那么onreadystatechange应该调用readyState为4的send吗?

如果上述情况不是预期的行为,我如何使用jasmine-ajax来验证是否调用了send方法?

以下是正在测试的代码:

代码语言:javascript
复制
    Loader = (function() {

      var loadNames = function(url, success_callback, error_callback) {

        var ajax = new XMLHttpRequest();

        ajax.open("GET", url);
        ajax.onreadystatechange = function () {
          console.log("Ready state is " + ajax.readyState);
          if (ajax.readyState === 4 && ajax.status === 200) {
            success_callback(JSON.parse(ajax.responseText));
          } else if (ajax.readyState === 4 && ajax.status !== 200) {
            error_callback("There was a problem. Status returned was " + ajax.status);
          }
        };

        ajax.onerror = function () {
         error_callback("Unknown error");
        };

        // Shouldn't removing the call to send prevent
        // onredystatechange from being called with readyState 4?
        // ajax.send();
      };

      return {
        loadNames: loadNames
      };

    })();

下面是测试结果:

代码语言:javascript
复制
describe("Loader", function () {

  var successFunction, failFunction;

  beforeEach(function () {
    jasmine.Ajax.install();
    successFunction = jasmine.createSpy("successFunction");
    failFunction = jasmine.createSpy("failFunction");
  });

  afterEach(function () {
    jasmine.Ajax.uninstall();
  });

  describe("#loadNames", function () {    
    it("Makes a success callback with the data when successful", function () {
      Loader.loadNames("someURL", successFunction, failFunction);
      jasmine.Ajax.requests.mostRecent().respondWith({
        "status": 200,
        "contentType": 'application/json',
        "responseText": '[1, 2, 4, 3, 5]'
      });

      // Shouldn't this fail since I never called send?
      expect(successFunction).toHaveBeenCalledWith([1, 2, 4, 3, 5]);
    });
  });
});

我很惊讶地看到successFunction被调用了,因为被测试的代码从来没有调用过ajax.send()。如果这是库的预期行为,那么如何对底层ajax对象执行spyOn操作,以便验证被测代码是否调用了send

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-11 04:15:34

是的,您没有调用ajax.send(),但是由于下面这段代码,您触发了ajax.onreadystatechange事件:

代码语言:javascript
复制
jasmine.Ajax.requests.mostRecent().respondWith({
  "status": 200,
  "contentType": 'application/json',
  "responseText": '[1, 2, 4, 3, 5]'
});

这会改变readystate并将readystate设置为done。这实际上正如文档中所说的那样:https://jasmine.github.io/2.6/ajax.html

至于如何检查xhr.send是否真的被调用,这个SO answer解释说,你可以在你的beforeEach中执行以下操作来监视它:

代码语言:javascript
复制
spyOn(XMLHttpRequest.prototype, 'send');

在加载器中取消对xhr.send()部分的注释后,您可以检查方法调用,如下所示:

代码语言:javascript
复制
describe("#loadNames", function () {    
  it("Makes a success callback with the data when successful", function () {
    Loader.loadNames("someURL", successFunction, failFunction);
    jasmine.Ajax.requests.mostRecent().respondWith({
      "status": 200,
      "contentType": 'application/json',
      "responseText": '[1, 2, 4, 3, 5]'
    });

    expect(XMLHttpRequest.prototype.open).toHaveBeenCalled();
  });
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55620423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档