如何使用npm库提供异步集成测试?

时间:2015-12-22 16:52:20

标签: javascript node.js npm integration-testing geojson

我维护a set of middleware生成有效的GeoJSON以便为its resulting service提供动力。有一个npm模块可以根据String或对象执行linting。由于中间件本身不在节点中,我希望有一个使用npm作为集成测试套件的中间件测试套件,确保每个端点都生成有效的GeoJSON。

我正在使用mocha,但除此之外,我对任何产生结果的框架持开放态度。 (我已经尝试过Q,q-io,异步数组和其他几个。)

这是我所瞄准的同步式写作:

describe('testPath()', function() {
    it("should be a function", function () {
      expect(geojsonLint.testPath).to.be.a('function')
    });

    it("should test a path", function () {
      var firstPath = geojsonLint.findEndpoints()[0];
      expect(geojsonLint.testPath(firstPath)).to.be.true
    });
});

然后,在此基础上,测试所有路径的同步版本可能如下所示:

describe('testAllPaths()', function() {
    it("should test a path", function () {
      geojsonLint.findEndpoints().map(function(path) {
         expect(geojsonLint.testPath(path)).to.be.true
      }
    });
});

我已经多次改变testPath的实施,但最具说明性的尝试如下:

  testPath: function (path, callback) {
    return request('http://localhost:5000/'+path, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        callback(geojsonhint.hint(body), error, response);
      } else {
        callback(body, error, response);
      }
    });
  }

我可以确保中间件在另一个端口上本地运行,如果请求成功,我希望将结果传递给geojsonhint.hint。最后,我想验证该通话的结果是空的。

到目前为止,我的努力是available,但我认为他们是穷人。

赞赏建立任何固定点。

1 个答案:

答案 0 :(得分:1)

使用testPath的现有实现,@rockbot能够帮助我直接使用mocha进行此测试调用:

  it("should test a path", function (done) {
    var firstPath = geojsonLint.findEndpoints()[0];
    geojsonLint.testPath(firstPath, function(r, e, resp) {
      expect(r).to.be.empty;
      done();
    });
  });

更改测试本身的签名并从回调内部调用它!