【问题标题】:Uncaught ValidationError; mongoose and mocha未捕获的验证错误;猫鼬和摩卡咖啡
【发布时间】:2015-07-18 00:49:18
【问题描述】:

我在 mongoose 架构上有一个实例方法,但我无法捕捉到 mocha 引发的错误。

该方法没有抛出,它以错误作为参数回调,但 mocha 测试没有捕捉到它,我得到一个未捕获的错误。

这是一个示例模块,使用 mongoose 来做某事作为一种方法:

var mongoose = require('mongoose');
var model;

function init(callback) {
  mongoose.connect('localhost/test', function() {

    var schema = new mongoose.Schema({
      a: String
    });
    schema.methods.act = function(param, cb) {

      if (!param) {
        console.log('Failing, no param.');
        return cb(new Error('Text'));
      }
      this.a = param;
      this.save(cb);
    };
    model = mongoose.model('schema', schema);
    callback();
  });
}

function run(cb) {
  var instance = new model();
  instance.save(function(err) {

    if (err) {
      throw err;
    }
    instance.act(null, function(err) {

      if (err) {
        console.log('An error:', err);
        cb(err);
      };
    });
  });
}

module.exports = {

  init: init,
  run: run
};

这是一个简化的摩卡测试仪:

require('should');
var myModule = require('./testm');
describe('test', function() {

  before(function(done) {
    // prep stuff
    myModule.init(done);
  });

  it('should catch the error', function(done) {

    myModule.run(function(err) {

      console.log('Error here:', err);
      err.message.should.equal('Text');
      done();
    });
  });
});

运行测试没有按预期工作:

mocha test


  test
Failing, no param.
An error: [Error: Text]
Error here: [Error: Text] Text
    1) should catch the error


  0 passing (30ms)
  1 failing

  1) test should catch the error:
     Uncaught TypeError: Cannot call method 'equal' of undefined
      at /home/zlatko/tmp/test.js:14:26
      at /home/zlatko/tmp/testm.js:35:9
      at model.schema.methods.act (/home/zlatko/tmp/testm.js:14:16)
      at Promise.<anonymous> (/home/zlatko/tmp/testm.js:31:14)
      at Promise.<anonymous> (/home/zlatko/tmp/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
      at Promise.emit (events.js:98:17)
      at Promise.emit (/home/zlatko/tmp/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
      at Promise.fulfill (/home/zlatko/tmp/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
      at handleSave (/home/zlatko/tmp/node_modules/mongoose/lib/model.js:133:13)
      at /home/zlatko/tmp/node_modules/mongoose/lib/utils.js:408:16
      at /home/zlatko/tmp/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection/core.js:128:9
      at /home/zlatko/tmp/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1195:7
      at /home/zlatko/tmp/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1903:9
      at Server.Base._callHandler (/home/zlatko/tmp/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
      at /home/zlatko/tmp/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:487:18
      at MongoReply.parseBody (/home/zlatko/tmp/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
      at null.<anonymous> (/home/zlatko/tmp/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:445:20)
      at emit (events.js:95:17)
      at null.<anonymous> (/home/zlatko/tmp/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:207:13)
      at emit (events.js:98:17)
      at Socket.<anonymous> (/home/zlatko/tmp/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:440:22)
      at Socket.emit (events.js:95:17)
      at Socket.<anonymous> (_stream_readable.js:765:14)
      at Socket.emit (events.js:92:17)
      at emitReadable_ (_stream_readable.js:427:10)
      at emitReadable (_stream_readable.js:423:5)
      at readableAddChunk (_stream_readable.js:166:9)
      at Socket.Readable.push (_stream_readable.js:128:10)
      at TCP.onread (net.js:529:21)

我做错了什么?

更新:改得更清楚了。

【问题讨论】:

  • 好吧,不知何故你的require('should') 工作不正常。您是否尝试将其设置为变量,如文档建议的那样:var should = require('should')?另一件事:您之前在问题中描述的第一个错误是基于我的回答。当您删除该部分时,我的回答就没有意义了。你应该考虑把那部分放回去,同时保持你的更新,这样其他人就可以理解整个上下文。

标签: node.js mongodb error-handling mongoose mocha.js


【解决方案1】:

在我看来,@Louis 的回答是在异步场景中检查错误的最佳方式:只要断言它是否存在、它们的类型等等。

但是,如果您真的想在回调中收到错误时抛出错误,可以使用chai-as-promised 来实现,例如:

it('should catch the error', function() {

  return Promise.resolve(function () {
    myModule.run(function(err) {

      if (err) {throw err;}
    });
  }).should.eventually.throw(Error);
});

在我的示例中,我使用should.js 作为断言库,但您可以使用任何您想要的断言库。

【讨论】:

    猜你喜欢
    • 2013-05-24
    • 2012-12-03
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    相关资源
    最近更新 更多