如何使用mocha使用'done();'进行异步测试?

Adr*_*ron 3 javascript testing asynchronous mocha.js node.js

我正在尝试使用该调用编写asynchronous测试。到目前为止,这是我的代码。mochadone();

it('should have data.', function () {
    db.put(collection, key, json_payload)
        .then(function (result) {
            result.should.exist;
            done();
        })
        .fail(function (err) {
            err.should.not.exist;
            done();
        })
})
Run Code Online (Sandbox Code Playgroud)

但是,结果是代码仅在不等待 then的情况下执行,否则将无法真正返回结果。是否done();需要在代码中的其他位置?

还在此处发布了整个仓库:https : //github.com/Adron/node_testing_testing

Not*_*elf 5

如果要进行异步测试,则需要处理完成的参数

it('should have data.', function (done) {
    db.put(collection, key, json_payload)
        .then(function (result) {
            result.should.exist;
            done();
        })
        .fail(function (err) {
            err.should.not.exist;
            done();
        })
})
Run Code Online (Sandbox Code Playgroud)

同样,如果您使用Q作为Promise库,您可能希望像这样完成链。

it('should have data.', function (done) {
    db.put(collection, key, json_payload)
        .then(function (result) {
            result.should.exist;
        })
        .fail(function (err) {
            err.should.not.exist;

        })
        .done(done,done)
})
Run Code Online (Sandbox Code Playgroud)