Node.js Nodejs断言未执行到Mocha测试中
我面临一个奇怪的问题,我为我的模块编写了多个测试,但最后一个测试不起作用。我测试一个异步函数,除了测试失败之外,一切都很好。我不知道为什么,但是assert.equal后面的代码没有执行。如果测试成功,则正确调用doneNode.js Nodejs断言未执行到Mocha测试中,node.js,unit-testing,assert,mocha.js,Node.js,Unit Testing,Assert,Mocha.js,我面临一个奇怪的问题,我为我的模块编写了多个测试,但最后一个测试不起作用。我测试一个异步函数,除了测试失败之外,一切都很好。我不知道为什么,但是assert.equal后面的代码没有执行。如果测试成功,则正确调用done describe('download', function(){ it('should download pictures from given URLs', function(done){ Download.download(list, functi
describe('download', function(){
it('should download pictures from given URLs', function(done){
Download.download(list, function(){
var pictures = fs.readdirSync("exports/merge/");
console.log('before assert - '+pictures.length);
assert.equal(pictures.length, 3);
console.log('before done - '+pictures.length);
done();
});
});
});
打印日志'before assert',则不会发生任何事情。
如果I pictures.length等于3,则正确调用done()。为了抛出它们,最简单的解决方案是使用
.done()
结束承诺链
describe('download', function(){
it('should download pictures from given URLs', function(done){
Download.download(list, function(){
var pictures = fs.readdirSync("exports/merge/");
console.log('before assert - '+pictures.length);
assert.equal(pictures.length, 3);
console.log('before done - '+pictures.length);
done();
});
});
});
有关此主题的更多信息,请参阅:
当你到达承诺链的末端时,你应该要么返回最后一个承诺,要么结束承诺链。由于处理程序捕获错误,因此异常可能无法被观察到,这是一种不幸的模式。
因此,要么返回它,要么结束它
第一个
控制台.log
为图片.length
输出什么?可能是未定义的
或其他东西..不,它是一个int,如果它是3,一切正常,当它是一个int而不是3时会发生错误。它会输出什么消息?似乎不会显示此测试中发生的任何错误,执行将等待超时。我不知道,是范围错误还是其他什么。好吧,问题是我的下载函数中的承诺。摩卡无法找回错误,因为它们被扔进了承诺。当我找到检索错误的方法时,将发布一个aswer