【问题标题】:Mocha Unit Testing Mongoose ModelsMocha 单元测试 Mongoose 模型
【发布时间】:2016-11-19 04:23:33
【问题描述】:

我正在努力找出为我的 NodeJS 应用程序编写这些单元测试的正确方法(即不是 hack)。

在 server.js 中,我将 mongoose 连接到在 localhost:27017 上运行的数据库。当我运行 mocha 测试时,我想连接到在 localhost:37017 上运行的 不同 mongoDB 实例,这样我就不会针对实时数据库运行测试。当我在 test.js 中需要 mongoose 并尝试连接时,mongoose 会抛出错误,说“尝试打开未关闭的连接。”

我曾尝试关闭 test.js 中的当前连接,但由于某种原因它不起作用。

我的问题是:在一个文件中连接到测试数据库的正确方法是什么,但继续让 server.js 连接到实时数据库?

我的代码如下:

// test.js
var app = require('../lib/server') // This connects mongoose to a database
var assert = require('assert');
var httpstatus = require('http-status');
var superagent = require('superagent');

// Connect to mongoose
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:37017/testDB'); // THIS THROWS ERROR because server.js is connecting to localhost:27017/liveDB

// Models we will be testing
var thing = require('../models/thing.js');

describe('Thing', function() {

    before(function() {
        // Clear the database here
    }

    beforeEach(function() {
        // Insert, modify, set up records here
    }

    it('saves the thing to the database', function() {
        // Save and query a thing here (to the test DB)
    });
});

【问题讨论】:

  • 一般来说,这些东西是在你的应用程序之外配置的,所以你会在测试时使用不同的配置。但作为一个快速破解(抱歉...),您可以在测试文件中调用 .connect() 之前尝试查看 mongoose.disconnect() 是否有效。
  • 即使在我连接到数据库之前使用mongoose.disconnect(),它仍然会引发“尝试打开未关闭的连接”错误。您能否详细说明如何设置您提到的配置文件?我是一个非常新的(不到 2 周)节点开发人员

标签: node.js mongodb unit-testing mongoose mocha.js


【解决方案1】:

你可以试试这个(虽然它是一个 hack):

// Connect to mongoose
var mongoose = require('mongoose');
before(function(done) {
  mongoose.disconnect(function() {
    mongoose.connect('mongodb://localhost:37017/testDB');
    done();
  });
});

// Models we will be testing (see text)
var thing = require('../models/thing.js');

...

describe(...)

可能还需要将模型加载到 disconnect 处理程序中,否则它可能会“附加”到原始连接。

同样,这仍然是一个 hack,我建议将您的数据库配置移动到某种外部配置文件中,或者使用环境变量,这可能相对容易实现:

// server.js
mongoose.connect(process.env.MONGO_URL || 'mongodb://localhost:27017/prodDB')

// test.js
process.env.MONGO_URL = 'mongodb://localhost:37017/testDB'
var app = require('../lib/server');

【讨论】:

    【解决方案2】:
    npm i env-cmd --save   // install this package
    

    在项目根目录下创建config目录,并在config文件夹中创建dev.env和test.env文件。

    在 test.env 文件中,像这样配置你的测试环境:

    PORT=4000
    
    MONGODB_URL=mongodb://127.0.0.1:27017/dbTestName
    

    然后在package.json中,将test.env的设置加载到测试环境中。

    "test": "env-cmd  -f ./config/test.env jest --watchAll --runInBand"
    

    这里我们需要模块,然后在这里加载 test.env 设置。

    env-cmd 软件包将根据您的环境将 .env 文件中的所有设置添加到 process.env 对象中。当您运行run npm test 时,您的应用将使用 test.env 中的设置。

    所以在您的应用程序中,您应该使用 MONGODB_URL 连接 mongoose。

    mongoose
      .connect(process.env.MONGODB_URL, {
        useNewUrlParser: true,
        useCreateIndex: true,
        useFindAndModify: false
      })
      .catch(err => {
        console.log(err.message);
        process.exit(1);
      })
      .then(() => {
        console.log("connected to mongodb");
      });
    

    您将为您的开发环境使用 dev.env 中的设置。现在你需要调整 package.json 中的开发脚本

    "dev": "env-cmd -f ./config/dev.env node src/index.js",
    

    现在当你运行时

    npm run dev
    

    您的应用从 dev.env 加载设置

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2019-05-04
      • 2017-10-27
      • 2017-06-10
      • 2013-08-12
      • 2015-06-02
      • 2014-05-17
      • 2015-05-11
      • 1970-01-01
      相关资源
      最近更新 更多