gpt4 book ai didi

node.js - 未捕获的验证错误; Mongoose 和 Mocha

转载 作者:行者123 更新时间:2023-12-03 09:06:46 26 4
gpt4 key购买 nike

我在 Mongoose 模式上有一个实例方法,但我无法捕捉到它用 mocha 引发的错误。

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

这是一个示例模块,使用 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
};

这是一个简化的 Mocha 测试仪:
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)

我做错了什么?

更新:更改为更清晰。

最佳答案

在我看来,@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作为断言库,但你可以使用任何你想要的断言库。

关于node.js - 未捕获的验证错误; Mongoose 和 Mocha ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30101206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com