lundi 23 février 2015

NodeUnit - Testing an asynchronous function

I want to tests the results returned by an asynchronous function with nodeunit. I have the following test :



module.exports = {
createFail: function(assert) {
var user = new CRUDUser({ firstName: 'Node' });

assert.equal(undefined, user.id);

user.create(function(res) { // user.create() makes an async AJAX call and should call this function back
assert.equal(false, res.success);
assert.equal(undefined, user.id); // user not created cause 'lastName' can't be null

console.log('#####');
assert.done();
});
}
};


But when I run it I get the following error :



FAILURES: Undone tests (or their setups/teardowns):
- createFail

To fix this, make sure all tests call test.done()


If I move the assert.done(); call outside the callback function, the test ends up before the AJAX call has been made.


I also tried to add assert.expect(3); at the very beginning of the test to make it "wait" until the callback function calls assert.done(); but I get the same error as above.


In all cases, the expected ##### isn't outputed to the console obviously. What am I doing wrong ??


Aucun commentaire:

Enregistrer un commentaire