【问题标题】:How to correct the “connection.readyState” of mongoose in mocha from “connecting” to “connected?如何纠正 mocha 中 mongoose 的“connection.readyState”从“正在连接”到“已连接”?
【发布时间】:2014-02-05 06:43:36
【问题描述】:

当我尝试运行 mocha 测试时,我得到 “无法确定服务器状态” 因为猫鼬连接处于连接状态。

请建议如何处理这种情况。

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test');

console.log('conn ready:  '+mongoose.connection.readyState);

// "conn ready: 2"  i.e connecting for test case as well as from user register form 


var Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId,

    UserSchema = new Schema({

       // schemas

    });


UserSchema.statics.newUser = function (uname, email, pass) {

    var instance = new User();

    instance.uname  = uname;

    instance.email  = email;

    instance.pass   = pass;

    console.log("conn state: "+mongoose.connection.readyState); 

    // "conn state: 2"  i.e connecting for test case. But  1  i.e connected for  user register form 

    instance.save(function (err) {
      // Do rest
    });

};

var User = mongoose.model('User', UserSchema);
exports.User = User;

【问题讨论】:

    标签: node.js mongoose mocha.js


    【解决方案1】:

    数据库连接是异步发生的,因此当您在调用connect 后直接检查它时,它可能会报告它仍在连接。如果你想在连接后做一些事情,你需要传入一个回调

    mongoose.connect('mongodb://localhost/test', function (error) {
      // Do things once connected
    });
    

    至于如何在您的场景中处理它,我的建议是从您的模型中分离连接并在需要时连接到 MongoDB

    所以如果你在 mocha 中测试你的用户模型,这可以在 Before 钩子中完成

    var mongoose = require("mongoose");
    
    // Load in your user model wherever that is
    var User = require("../models/user");
    
    describe("My user model tests", function () {
      // Connect to mongodb here before you start testing
      before(function (done) {
        mongoose.connect('mongodb://localhost/test', function (error) {
          if (error) throw error; // Handle failed connection
          console.log('conn ready:  '+mongoose.connection.readyState);
          done();
        });
      });
    
      // And include disconnect afterwards
      after(function (done) {
        mongoose.disconnect(done);
      });
    
      // Test your user model down here
      it("passes some tests");
    });
    

    根据您构建应用程序的方式,我建议您将数据库连接移至合理的位置(例如服务器配置)。当您测试整个应用程序(例如集成测试)时,您将在 Before 挂钩中启动您的服务器

    【讨论】:

      【解决方案2】:

      另外,如果你的 mongoose 连接例程在另一个文件中(比如我),在 JS 的异步特性期间,你应该稍等片刻,等待 readyState 被“连接”(1)。我在“之前”解决了它:

      var app = require('../server'); //I have mongoose connection in this file
      var mongoose = require('mongoose');
      
      var context = describe;
      
      describe('Initial DB routines', function() {
      
        before(function (done) {
          mongoose.connection.on('connected', done);
        });
      
        it('Should connect to MongoDB', function(done) {
           mongoose.connection.readyState === 1 ? done(): done(false);
        });
      });
      

      【讨论】:

        猜你喜欢
        • 2013-04-15
        • 1970-01-01
        • 2015-07-30
        • 2020-12-05
        • 1970-01-01
        • 1970-01-01
        • 2017-01-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多