如何使用npm库提供asynchronous集成testing?

我维护一组生成有效GeoJSON所需的中间件 ,以便为其生成的服务提供支持 。 有一个npm模块,可以执行基于string或对象的linting。 由于中间件本身不在节点中,所以我希望有一个中间件的testing套件,它使用npm作为集成testing套件,确保每个端点都生成有效的GeoJSON。

我正在使用摩卡,但除此之外,我对任何产生结果的框架都是开放的。 (我已经尝试了Q,q-io,asynchronous数组和其他几个。)

这是一个我正在瞄准的同步式写法:

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 }); }); 

然后,在此基础上,testing所有path的同步版本可能如下所示:

 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。 最终,我想validation那个调用的结果是空的。

迄今为止我的努力都是可以的 ,但是我认为他们是穷人。

任何build立固定点赞赏。

通过现有的testPath实现, @ rockbot能够直接帮助我使用mocha来进行这个testing调用:

  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(); }); }); 

改变testing本身的签名,并从callback中调用它!