简体   繁体   English

Nodejs断言未执行到Mocha测试中

[英]Nodejs Assert not executing into Mocha tests

I face a strange problem, I wrote multiple tests for my module but the last one is not working. 我遇到一个奇怪的问题,我为我的模块编写了多个测试,但最后一个不起作用。 I test an async function, everything work great, except if the test fail. 我测试了一个异步功能,一切正常,除非测试失败。 I don't know why but the code after the assert.equal is not executed. 我不知道为什么,但是assert.equal之后的代码没有执行。 If the test succeed, done is correctly called. 如果测试成功,则正确调用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();
        });
    });
});

The log 'before assert' is printed, then nothing happen. 打印“断言之前”的日志,然后什么也没有发生。 If I pictures.length equal 3, done() is correctly called. 如果我pictures.length等于3,则正确调用done()。

Promises swallow errors. 承诺吞下错误。 In order for them to be thrown, the easiest solution is to end the promise chain with .done() . 为了抛出它们,最简单的解决方案是使用.done()结束promise链。

    var p = 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();
    });
    p.done();

See for more information bluebird's documentation or Q's documentation on the subject: 有关更多信息,请参阅bluebird的文档Q的主题文档

When you get to the end of a chain of promises, you should either return the last promise or end the chain. 当您到达承诺链的末尾时,您应该返回最后一个承诺或结束该链。 Since handlers catch errors, it's an unfortunate pattern that the exceptions can go unobserved. 由于处理程序会捕获错误,因此不幸的是,这种异常无法观察到。 So, either return it or end it . 因此,要么返回它,要么结束它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM