Uncaught ValidationError;猫鼬和摩卡

时间:2015-05-07 12:33:33

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

我在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)

我可以做错什么?

更新:更改为更清晰。

1 个答案:

答案 0 :(得分: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作为断言库,但您可以使用所需的任何断言库。

相关问题