【发布时间】:2013-05-12 12:05:07
【问题描述】:
在我的节点应用程序中,我使用 mocha 来测试我的代码。使用 mocha 调用许多异步函数时,出现超时错误 (Error: timeout of 2000ms exceeded.)。我该如何解决这个问题?
var module = require('../lib/myModule');
var should = require('chai').should();
describe('Testing Module', function() {
it('Save Data', function(done) {
this.timeout(15000);
var data = {
a: 'aa',
b: 'bb'
};
module.save(data, function(err, res) {
should.not.exist(err);
done();
});
});
it('Get Data By Id', function(done) {
var id = "28ca9";
module.get(id, function(err, res) {
console.log(res);
should.not.exist(err);
done();
});
});
});
【问题讨论】:
-
是集成测试吗?运行测试需要很长时间 - 也许您应该考虑存根 - github.com/thlorenz/proxyquire 可能会对您有所帮助。
-
@surui 谢谢你,我会看的
-
我可以推荐对异步的东西使用 Promise,然后使用 Chai as promise 测试它是轻而易举的事