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();
})
})
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?
Also posted the whole repo right here:
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();
})
})
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?
Also posted the whole repo right here: https://github./Adron/node_testing_testing
Share Improve this question edited Jan 17, 2014 at 5:50 Has AlTaiar 4,1622 gold badges39 silver badges37 bronze badges asked Jan 16, 2014 at 23:34 AdronAdron 1,8763 gold badges21 silver badges41 bronze badges 4-
Correct me if I'm wrong, but doesn't the function you pass to
it
call need to have thedone
parameter?it('should have data.', function(done) {
vs.it('should have data.', function() {
. – juanpaco Commented Jan 17, 2014 at 1:16 - possible duplicate of Testing asynchronous function with mocha – Louis Commented Jan 17, 2014 at 12:58
- @juanpaco yes, check out the answer below by notmyself. – Adron Commented Mar 16, 2014 at 2:51
- if the tests executes "without waiting" doesn't mean it is asynchronous? – ovi Commented Aug 3, 2016 at 8:28
2 Answers
Reset to default 5if 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();
})
})
also if you are using Q as your promise library you might want to plete 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)
})
I think you mean to actually call the done()
callback.
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();
})
})
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744891135a4599419.html
评论列表(0条)