繁体   English   中英

NodeUnit-测试异步功能

[英]NodeUnit - Testing an asynchronous function

我想用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(); 但我收到与上述相同的错误。

在所有情况下,预期的#####显然不会输出到控制台。 我究竟做错了什么 ??

根据您的输出,我的第一个理论是user.create根本不会调用回调函数。 进行调试,并确保在所有情况下(成功,失败,超时等),它实际上都调用了回调函数。 换句话说,您的测试代码看起来可能不错,但是您正在测试的代码可能存在错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM