使用mongoose createConnection进行超时测试,使用mocha进行测试

时间:2015-10-10 09:55:23

标签: node.js mongoose 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 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呢?

感谢您的帮助:)

1 个答案:

答案 0 :(得分:1)

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

  

如果包含已注册的模型,则需要始终引用该连接变量;否则,如果使用mongoose加载模型,它将永远不会实际与数据库通信。

要解决您的问题,请先将连接实例传递给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');
}