为什么会出现“错误:解决方法指定过多"? [英] Why am I getting "Error: Resolution method is overspecified"?

查看:36
本文介绍了为什么会出现“错误:解决方法指定过多"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级后,Mocha甚至无法运行简单的测试,这里是代码

After the upgrade, Mocha can not even run a simple test here is the code

const assert = require('assert');

it('should complete this test', function (done) {
  return new Promise(function (resolve) {
    assert.ok(true);
    resolve();
   })
  .then(done);
});

我从此处获取了此代码

我知道它现在会引发异常Error: Resolution method is overspecified. Specify a callback * or * return a Promise; not both.

I understood that it now throws an exception Error: Resolution method is overspecified. Specify a callback * or * return a Promise; not both.

但是如何使其工作呢?我不明白.我有

But how to make it work? I did not understand. I have

node -v 6.9.4

mocha -v 3.2.0

现在如何以正确的新格式运行此代码?

推荐答案

只需删除
.then(done);并将function(done)替换为function()

Just drop
.then(done); and replace function(done) with function()

您返回的是Promise,所以调用完是多余的,如错误消息中所述

You are returning a Promise so calling done is redundant as it said in error message

在较旧的版本中,必须使用回调,以免发生类似的异步方法

In the elder versions you had to use callback in case of async methods like that

it ('returns async', function(done) {
   callAsync()
   .then(function(result) {
      assert.ok(result);
      done();
   });
})

现在,您可以选择返回诺言

Now you have an alternative of returning a Promise

it ('returns async', function() {
  return new Promise(function (resolve) {
     callAsync()
       .then(function(result) {
          assert.ok(result);
          resolve();
       });
  });
})

但同时使用这两种方法会产生误导 (例如,请参见此处 https://github.com/mochajs/mocha/issues/2407)

But using both is misleading (see for example here https://github.com/mochajs/mocha/issues/2407)

这篇关于为什么会出现“错误:解决方法指定过多"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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