摩卡单位testing路线返回200好

我正在使用摩卡来testing,如果我已经添加到我的服务器的一些路线返回200好,如果响应包含一个string。 我这样做是这样的:

var testDBRoute = function(link, routeName) { it('should return 200 OK for ' + routeName, function (done) { http.get(link, function (res) { assert.equal(200, res.statusCode); done(); }) }); it('should return the correct info', function(done) { http.get(link, function (res) { var body = ''; res.on('data', function(chunk) { body += chunk; }); res.on('end', function() { var response = JSON.parse(body); assert.equal('abc', response.id); done(); }); }); } ) }; 

我用同样的方法调用这个函数6次:

 describe('/link1', function () { var link = HOST + '/api/link1/'; testDBRoute(link, 'link1'); }); 

无论请求的顺序,它总是在第8次testing失败,这意味着它是8个http获取请求,然后失败,这个错误:

错误:空值超过2000毫秒超时。 (timers.js:112:15)在Timer.listOnTimeout [astimeout](\ node_modules \ mocha \ lib \ runnable.js:158:19)

我必须指定我在Windows上运行testing。

我已经解决了这个问题,使用superTest来代替这样的http

 var request = require('supertest'); var app = require('../server'); // app is the express context from the server describe('GET /api', function () { it('test api', function (done) { request(app) .get('/api') .end (function (err, res) { assert.equal(200, res.statusCode); done(); }); }); }); 

所以看起来问题是在http模块…