我正在使用supertest with async来测试一些网站。 当我使用mocha执行测试时,不会输入结束函数。 我在末尾函数中添加了一些日志记录,但它没有打印出来。 当我删除async.each时,输入了结束函数 ,因为我看到了日志记录。知道什么似乎是错的吗? supertest与async不兼容吗?
这是我的代码:
describe('Era', function() {
it('Does era run?', function(done) {
async.each(config.url.era, function(url) {
console.log('Opening connection to ' + url);
request(url).get('/rest/status').expect(200).end(function(err, res) {
console.log(err);
if(err) return done(err);
console.log('Time to assert some stuff');
assert.equal(res.text.indexOf('up') > -1, 'Node ' + url + ' failed with status ' + res.statusCode);
});
});
done();
});
答案 0 :(得分:0)
我认为您需要调用回调done
:
it('Does era run?', function(done) {
async.each(config.url.era, function(url) {
console.log('Opening connection to ' + url);
request(url).get('/rest/status').expect(200).end(function(err, res) {
console.log(err);
if(err) return done(err);
console.log('Time to assert some stuff');
assert.equal(res.text.indexOf('up') > -1, 'Node ' + url + ' failed with status ' + res.statusCode);
});
// call at end of each async call
done();
});
});
答案 1 :(得分:0)
async.each()期望每次迭代的回调继续。此外,async.each()末尾的可选回调可用于调用done()。以下是修订后的代码:
app/view/dashboard.html
或以上代码可以简化为:
describe('Era', function() {
it('Does era run?', function(done) {
async.each(config.url.era, function(url, eachCb) {
console.log('Opening connection to ' + url);
request(url).get('/rest/status').expect(200).end(function(err, res) {
console.log(err);
if (err) return eachCb(err); // terminate the loop
console.log('Time to assert some stuff');
assert.equal(res.text.indexOf('up') > -1, 'Node ' + url + ' failed with status ' + res.statusCode);
eachCb(null); // continue with the next iteration
});
}, function(err) {
if (err) {
return done(err);
}
done();
// the above 2 statements can be simplify for just
// done(err); // err is an error or null
});
});
});