Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

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

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

在marque.js:

"use strict";
let mongoose = require('mongoose'),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进行测试全部内容,希望文章能够帮你解决node.js – 使用mongoose createConnection进行超时测试,使用mocha进行测试所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: