done()回调有什么意义? [英] What is the point of the done() callback?

查看:312
本文介绍了done()回调有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mochajs中,他们使用done()来测试异步代码,如下所示:

In Mochajs, they use "done()" to test for asynchronous code, like so:

describe('User', function() {
  describe('#save()', function() {
    it('should save without error', function(done) {
      var user = new User('Luna');
      user.save(function(err) {
        if (err) throw err;
        done();
      });
    });
  });
});

这究竟是什么意思?我做了console.log(done.toString()),我得到了这个:

What does this mean exactly? I did console.log(done.toString()) and I got this:

function (err) {
  if (err instanceof Error || toString.call(err) === '[object Error]') {
    return done(err);
  }
  if (err) {
    if (Object.prototype.toString.call(err) === '[object Object]') {
      return done(new Error('done() invoked with non-Error: '
        + JSON.stringify(err)));
    }
    return done(new Error('done() invoked with non-Error: ' + err));
  }
  done();
}

这里的done()是否与done()不同在第一段代码?

Is the done() at the very end here different than the done() in the first piece of code?

推荐答案

Mocha能够处理同步和异步测试。当您运行同步测试时,您可以将其作为匿名函数传递给 it ,而您不必执行任何其他操作:Mocha知道测试结束时功能返回。但是,如果您正在运行异步测试,则必须告诉Mocha该测试是异步的。有两种方法可以执行此操作:

Mocha is able to handle synchronous and asynchronous tests. When you run a synchronous test, you can just pass it as an anonymous function to it and you don't have to do anything else: Mocha knows the test is over when the function returns. However, if you are running an asynchronous test, you have to tell Mocha that the test is asynchronous. There are two ways to do this:


  1. 声明传递给 it 的匿名函数接受一个参数。 Mocha将使用单个参数调用您的匿名函数,该函数必须调用以指示您的测试结束。 (由于传统,此参数称为已完成。您可以将其称为完成 cb platypus 并且它可以正常工作。)如果你打电话完成没有值,测试成功。使用值时,测试失败,值应为错误对象或从错误派生的对象。

  1. Declare that the anonymous function you pass to it takes a parameter. Mocha will call your anonymous function with a single parameter which is a function you must call to indicate that your test is over. (This parameter is called done due to tradition. You could call it complete, cb or platypus and it would work just the same.) If you call done without a value, the test is successful. With a value, the test is a failure and the value should be an Error object or an object derived from Error.

返回承诺:Mocha将等待承诺得到解决或拒绝。如果已解决,则测试成功。如果被拒绝,则测试失败。

Return a promise: Mocha will wait for the promise to be resolved or rejected. If resolved, the test is successful. If rejected, the test failed.

执行 done.toString时看到的代码()只是当你声明它接受参数时Mocha传递给你的测试的函数的代码。您可以在其中看到我上面提到的一些内容(例如,如果您将参数传递给已完成它应该是错误或派生自错误)。 已完成还有另一个已完成的函数,该函数是Mocha专用的。

The code you see when you do done.toString() is just the code of the function that Mocha passes to your test when you declare it to take a parameter. You can see in it some of what I mentioned above (e.g. if you pass a parameter to done it should be an Error or derived from Error). The done in there is another done function which is private to Mocha.

这篇关于done()回调有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆