测试表达。在自己的环境中使用假db

时间:2022-05-05 02:34:03

I've got the following structure for my web app

我的web应用程序有以下结构

db.js:

db.js:

mongoose = require('mongoose');
db = mongoose.createConnection("...");
playerSchema = new mongoose.Schema({
  // my schema stuff
});
exports.Player = db.model("player", playerSchema);

I have all my routes in an own file (route/index.js):

我的所有路线都在自己的文件中(route/index.js):

db = require('../db');

exports.createPlayer = function(req, res) {
  player = new db.Player();
  player.name = req.body.player_name;
  player.save(function(err) {
    if (err) {
      log(err);
    }
    res.redirect("/");
  });
};

app.js

app.js

routes = require('./routes');
app.post("/start", routes.createPlayer);

I test the app using mocha, should and supertest like the following example

我使用mocha来测试应用程序,应该和超级测试类似如下示例

should = require('should');
supertest = require('supertest');
app = require('../app');

describe("POST /start", function() {
  it("should redirect", function(done) {
    supertest(app)
    .post("/start")
    .send({
      player_name: "John Wayne"
    }).end(function(err, res) {
      should.not.exist(err);
      done();
    });
  });
});

It all works fine but I don't want to test against my production database. Does anyone know a smart way to use a dummy/different db just for testing? Maybe using a different environment? I'm kind of stuck!

这些都很好,但是我不想对我的产品数据库进行测试。有没有人知道一种聪明的方法来使用虚拟/不同的db来进行测试?也许使用不同的环境?我有点困!

1 个解决方案

#1


3  

I have a common.js file that is the first file that I include in my test files. At the top is this:

我有一个共同之处。js文件,这是我在测试文件中包含的第一个文件。顶部是这样的:

process.env.NODE_ENV    = 'test'
process.env.MONGOHQ_URL = 'mongodb://localhost/project-testing'

And when I do my database connection I have it like this:

当我进行数据库连接时,我的数据是这样的:

var dbUri = process.env.MONGOHQ_URL || 'mongodb://localhost/project'
var db    = mongoose.connect(dbUri)

#1


3  

I have a common.js file that is the first file that I include in my test files. At the top is this:

我有一个共同之处。js文件,这是我在测试文件中包含的第一个文件。顶部是这样的:

process.env.NODE_ENV    = 'test'
process.env.MONGOHQ_URL = 'mongodb://localhost/project-testing'

And when I do my database connection I have it like this:

当我进行数据库连接时,我的数据是这样的:

var dbUri = process.env.MONGOHQ_URL || 'mongodb://localhost/project'
var db    = mongoose.connect(dbUri)