gpt4 book ai didi

node.js - 使用 mongoose createConnection 超时以使用 mocha 进行测试

转载 作者:搜寻专家 更新时间:2023-10-31 23:24:38 25 4
gpt4 key购买 nike

我对 mongoose.createConnection 函数有疑问,这是我的测试代码:

"use strict";
// connect to mongodb://localhost/node_marque_test
// empty database before each test

let mongoose = require('mongoose'),
expect = require('chai').expect,
// use a specific base for test purposes
dbURI = 'mongodb://localhost/node_marque_test',
Marque = require('../lib/marque.js');

before(function(done){
// connect to db
let connection = mongoose.createConnection(dbURI);
// remove all documents
connection.on('open', function(){

Marque.remove(function(err, marques){
if(err){
console.log(err);
throw(err);
} else {
// console.log('cleaning marques from mongo');
done();
}
})
})
})
afterEach(function(done){
Marque.remove().exec(done);
})

describe('an instance of Marque', ()=>{
let marque;
beforeEach((done)=>{
marque = new Marque({name: 'YAMAHA'})
marque.save((err)=>{
if(err){throw(err);}
done();
})
})
it('has a nom', ()=>{
expect(marque.name).to.eql('YAMAHA');
})

it('has a _id attribute', ()=>{
expect(marque).to.have.property('_id')
})
})

下面是 Marque 对象的代码:

"use strict";
let mongoose = require('mongoose'), Schema = mongoose.Schema;

// Schema definition with some validation
let marqueSchema = Schema({
name: { type: String, required: true}
});

// compile schema to create a model
let Marque = mongoose.model('Marque', marqueSchema);

// custom validation rules
Marque.schema.path('name').validate(name_is_unique, "This name is already taken");

function name_is_unique(name,callback) {
Marque.find({$and: [{name: name},{_id: {$ne: this._id}}]}, function(err, names){
callback(err || names.length === 0);
});
}

module.exports = mongoose.model('Marque');

所以当我运行 npm test 时,我得到了这个错误:

  1) "before all" hook

0 passing (2s)
1 failing

1) "before all" hook:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

但是如果我替换

// connect to db
let connection = mongoose.createConnection(dbURI);
// remove all documents
connection.on('open', function(){

通过

// connect to db
mongoose.connect(dbURI);
// remove all documents
mongoose.connection.on('open', function(){

一切正常,测试通过:

  an instance of Marque
✓ has a nom
✓ has a _id attribute


2 passing (65ms)

问题是我需要做多次测试所以我不能使用mongoose.connect(否则我得到错误:尝试打开未关闭的连接。)

那么我如何在我的测试中使用 createConnection 连接到 mongoose?

感谢您的帮助:)

最佳答案

要解决此问题,我们需要在连接实例上注册模型架构。即使用 connection.model 而不是 mongoose.model。来自 here

you need to always reference that connection variable if you include a registered model, otherwise, if you use mongoose to load the model, it will never actually talk to your database.

要解决您的问题,请先将连接实例传递给 marque.js。

...
let connection = mongoose.createConnection(dbURI);
Marque = require('../lib/marque.js')(connection);
...

在 marque.js 中:

"use strict";
let mongoose = require('mongoose'), Schema = mongoose.Schema;

// Schema definition with some validation
let marqueSchema = Schema({
name: { type: String, required: true}
});

module.exports = function(conn) {
// compile schema to create a model. Probably should use a try-catch.
let Marque = conn.model('Marque', marqueSchema);

// custom model validation code here
// ...

return conn.model('Marque');
}

关于node.js - 使用 mongoose createConnection 超时以使用 mocha 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33052669/

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