如何使用摩卡做异步测试使用'进行();'? [英] How do I use mocha to do asynchronous tests using 'done();'?
问题描述
我正在试着写与摩卡
使用异步
测试完成();
电话。这是我的code为止。
I'm working on trying to write an asynchronous
test with mocha
using the done();
call. This is my code so far.
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();
})
})
结果却是,code只是执行无需等待作为当时还是无法与实际的结果返回。是否做();
必须在code内的不同地方。
The result however is that the code just executes without waiting for the then or fail to actually return with a result. Does done();
need to be at a different place within the code?
还贴出了整个回购在这里: https://github.com/Adron/node_testing_testing
Also posted the whole repo right here: https://github.com/Adron/node_testing_testing
推荐答案
如果你想异步测试,你需要处理进行了参数
if you want an async test you need to handle the done parameter
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();
})
})
此外,如果你正在使用q作为承诺库,您可能要完成链像这样。
also if you are using Q as your promise library you might want to complete your chain like so.
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)
})
这篇关于如何使用摩卡做异步测试使用'进行();'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!