简体   繁体   English

如何等待getJSON在Mocha测试中完成?

[英]How to wait for getJSON to finish in a Mocha test?

The below files are within a project created with the generator-webapp generator for Yeoman. 以下文件在使用generator-webapp generator为Yeoman创建的项目中。 My script is working in the browser and returns the information from the JSON file but does not work in the test most of the time. 我的脚本在浏览器中运行,并且从JSON文件返回信息,但是在大多数情况下在测试中不起作用。 The test succeeds sometimes which means the test hits a hiccup long enough to allow the getJSON to return the data in time. 该测试有时会成功,这意味着该测试会打long一段足够长的时间,以使getJSON能够及时返回数据。 In my search, I found various resources, here are two sources that sounded like they should be solving my issue: a stackoverflow question and a blog . 在搜索中,我发现了各种资源,这里有两个听起来像应该解决我的问题的资源:stackoverflow 问题blog

They both involve passing the done parameter to the it function and then calling done(); 它们都涉及将done参数传递给it函数,然后调用done(); after executing the test. 执行测试后。 At least, that is my understanding but it still isn't working. 至少,这是我的理解,但仍然无法正常工作。 I feel I am missing something really obvious. 我觉得我确实缺少一些明显的东西。

Here is app/scripts/source-data.js . 这是app/scripts/source-data.js

var source = (function() {
    var sourceData = null;

    _loadData();

    function _loadData(done) {
        $.getJSON("app/data/source.json", function(data) {
            sourceData = data;
        });
    }

    function getData() {
        return sourceData;
    }

    return {
        getData: getData
    };
})();

Here is test/spec/source-data.js . 这是test/spec/source-data.js

(function() {
    describe("Source Data", function() {
        describe("Data for External Modules", function() {
            it("returns the source data from a file", function(done){
                expect(source.getData().spec[0].name).to.equal("Spec");
                done();
            });
        });
    });
})();

I tried altering where done() is called as my understanding was that done() tells Mocha to go ahead with the rest of the test after the getJSON is done. 我试着更改了done()的调用位置,因为我的理解是done()告诉Mocha在getJSON完成后继续进行其余的测试。 However, at this point, this was just trial and error as I found I had no real understanding. 但是,在这一点上,这只是反复试验,因为我发现我没有真正的理解。

...
var data = source.getData();
done();
expect(data.spec[0].name).to.equal("Spec");
...

Following the above, I tried setTimeout in the main script but that still didn't work! 按照上面的方法,我在主脚本中尝试了setTimeout ,但是仍然无法正常工作! Even if it did, I don't think I should use setTimeout in this situation, but properly wait for the resolution of getJSON . 即使这样做,我也不认为在这种情况下应该使用setTimeout ,而是要适当地等待getJSON的解析。

You should use callback. 您应该使用回调。 For example: 例如:

var source = (function() {
  var sourceData;

  function getData(done) {
    if(sourceData){
      done(sourceData);
    } else {
      $.getJSON("app/data/source.json", function(data) {
        sourceData = data;
        done(data);
      });
    }
  }
  return {
    getData: getData
  };
})();

and test will be like this 测试会像这样

(function() {
  describe("Source Data", function() {
    describe("Data for External Modules", function() {
      it("returns the source data from a file", function(done){
        source.getData(function(sourceData){
          expect(sourceData.spec[0].name).to.equal("Spec");
          done();
        });
      });
    });
  });
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM