node.js – 使用mongoose createConnection进行超时测试,使用mocha进行测试

我有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测试时,我收到此错误:

  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');
}
点赞