【发布时间】:2015-04-25 04:44:14
【问题描述】:
我想用nodeunit 测试异步函数返回的结果。我有以下测试:
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();
});
}
};
但是当我运行它时,我收到以下错误:
FAILURES: Undone tests (or their setups/teardowns):
- createFail
To fix this, make sure all tests call test.done()
如果我将 assert.done(); 调用移到回调函数之外,则测试会在 AJAX 调用之前结束。
我还尝试在测试的最开始添加assert.expect(3);,使其“等待”直到回调函数调用assert.done();,但我得到与上面相同的错误。
在所有情况下,预期的##### 显然不会输出到控制台。我做错了什么??
【问题讨论】:
标签: javascript node.js unit-testing asynchronous nodeunit