Node.js 摩卡认为解析方法过于具体。指定回调*或*返回承诺;不是两者都有

Node.js 摩卡认为解析方法过于具体。指定回调*或*返回承诺;不是两者都有,node.js,mongoose,mocha.js,Node.js,Mongoose,Mocha.js,这是我在TDD的第一天 var mongoose = require("mongoose"), should = require('should'), User = require("app/models/user"); mongoose.connect('mongodb://localhost/altor-security'); describe('user data', function() { it('password should be different aft

这是我在TDD的第一天

var mongoose = require("mongoose"),
    should = require('should'),
    User = require("app/models/user");

mongoose.connect('mongodb://localhost/altor-security');

describe('user data', function() {
  it('password should be different after changing password', function(done) {
    var old_password_hash,
        new_password = "12345678";

    return User.findOne({ email: "example@gmail.com" }).exec()
    .then(function(user) {
      old_password_hash = user.password;
      return User.findOneAndUpdate({ _id : user._id }, { password: new_password }, { new: true }).exec();
    })
    .then(function(user) {
      user.password.should.not.equal(old_password_hash);
      done();
    })
    .catch(function(err) {
      err.should.equal(null);
      done();
    })
  });
})
我的测试失败,因为它认为User.findOneAndUpdate方法指定过度。但它确实需要三个参数:findCommand、update和options

你知道为什么会失败吗

谢谢

我的测试失败,因为它认为User.findOneAndUpdate方法指定过度

事实上,没有。它说“解析方法”(即代码告诉mocha异步测试完成的方式)是过度指定的

您正在使用回调并返回一个承诺,所以mocha无法告诉您的测试何时完成以及是否正常

您需要使用
done
或返回承诺。不是两者都有

优先方式(返回承诺)

我的测试失败,因为它认为User.findOneAndUpdate方法指定过度

事实上,没有。它说“解析方法”(即代码告诉mocha异步测试完成的方式)是过度指定的

您正在使用回调并返回一个承诺,所以mocha无法告诉您的测试何时完成以及是否正常

您需要使用
done
或返回承诺。不是两者都有

优先方式(返回承诺)

describe('user data', function() {
  it('password should be different after changing password', function(/*do not use done*/) {
    var old_password_hash,
        new_password = "12345678";

    // return a Promise
    return User.findOne({ email: "example@gmail.com" }).exec()
    .then(function(user) {
      old_password_hash = user.password;
      return User.findOneAndUpdate({ _id : user._id }, { password: new_password }, { new: true }).exec();
    })
    .then(function(user) {
      user.password.should.not.equal(old_password_hash);
    })
  });
})
describe('user data', function(done) {
  it('password should be different after changing password', function(done) {
    var old_password_hash,
        new_password = "12345678";

   // do not return anything
   User.findOne({ email: "example@gmail.com" }).exec()
    .then(function(user) {
      old_password_hash = user.password;
      return User.findOneAndUpdate({ _id : user._id }, { password: new_password }, { new: true }).exec();
    })
    .then(function(user) {
      user.password.should.not.equal(old_password_hash);
      done();
    })
    .catch(function(err) {
      err.should.equal(null);
      done();
    })
  });
})