Mongoose 不会删除数据库并在 Mocha 中正确关闭连接
Mongoose doesn't drop database and close connection properly in Mocha
我有两个简单的测试,但其中一个通过了,但另一个没有通过,因为架构再次被编译。
OverwriteModelError: Cannot overwrite CheckStaging
model once
compiled.
这是我通过的一项测试,因为它是 运行 第一。
var mongoose = require('mongoose'),
StagingManager = require('../lib/staging_manager'),
expect = require('expect.js');
describe('Staging manager', function() {
var StagingModel;
beforeEach(function(done) {
mongoose.connect('mongodb://localhost/BTest');
StagingModel = new StagingManager(mongoose).getStaging();
done();
});
describe('find one', function() {
it('should insert to database', function(done) {
// Do some test which works fine
});
});
afterEach(function (done) {
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close(function () {
done();
});
});
});
});
这是失败的测试
var StagingUtil = require('../lib/staging_util'),
StagingManager = require('../lib/staging_manager'),
mongoose = require('mongoose');
describe('Staging Util', function() {
var stagingUtil, StagingModel;
beforeEach(function(done) {
mongoose.connect('mongodb://localhost/DBTest');
StagingModel = new StagingManager(mongoose).getStaging();
stagingUtil = new StagingUtil(StagingModel);
done();
});
describe('message contains staging', function() {
it('should replace old user with new user', function(done) {
// Do some testing
});
});
afterEach(function (done) {
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close(function () {
done();
});
});
});
});
这是我的舞台经理
var Staging = function(mongoose) {
this.mongoose = mongoose;
};
Staging.prototype.getStaging = function() {
return this.mongoose.model('CheckStaging', {
user: String,
createdAt: { type: Date, default: Date.now }
});
};
module.exports = Staging;
mongoose.model
向 Mongoose 注册一个模型,因此您应该只调用一次而不是每次调用 getStaging 时。为您的分期模型尝试这样的事情:
var mongoose = require('mongoose');
var StagingModel = new mongoose.Schema({
user: String,
createdAt: { type: Date, default: Date.now }
});
mongoose.model('CheckStaging', StagingModel);
然后在你的消费代码中,使用
var mongoose = require('mongoose');
require('../lib/staging_manager');
var StagingModel = mongoose.model('CheckStaging');
require 只会执行一次,所以模型应该只用 mongoose 注册一次。
顺便说一句,对于单元测试,mockgoose 是一个出色的模拟 mongoose 的模拟库 - 值得研究!
我有两个简单的测试,但其中一个通过了,但另一个没有通过,因为架构再次被编译。
OverwriteModelError: Cannot overwrite
CheckStaging
model once compiled.
这是我通过的一项测试,因为它是 运行 第一。
var mongoose = require('mongoose'),
StagingManager = require('../lib/staging_manager'),
expect = require('expect.js');
describe('Staging manager', function() {
var StagingModel;
beforeEach(function(done) {
mongoose.connect('mongodb://localhost/BTest');
StagingModel = new StagingManager(mongoose).getStaging();
done();
});
describe('find one', function() {
it('should insert to database', function(done) {
// Do some test which works fine
});
});
afterEach(function (done) {
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close(function () {
done();
});
});
});
});
这是失败的测试
var StagingUtil = require('../lib/staging_util'),
StagingManager = require('../lib/staging_manager'),
mongoose = require('mongoose');
describe('Staging Util', function() {
var stagingUtil, StagingModel;
beforeEach(function(done) {
mongoose.connect('mongodb://localhost/DBTest');
StagingModel = new StagingManager(mongoose).getStaging();
stagingUtil = new StagingUtil(StagingModel);
done();
});
describe('message contains staging', function() {
it('should replace old user with new user', function(done) {
// Do some testing
});
});
afterEach(function (done) {
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close(function () {
done();
});
});
});
});
这是我的舞台经理
var Staging = function(mongoose) {
this.mongoose = mongoose;
};
Staging.prototype.getStaging = function() {
return this.mongoose.model('CheckStaging', {
user: String,
createdAt: { type: Date, default: Date.now }
});
};
module.exports = Staging;
mongoose.model
向 Mongoose 注册一个模型,因此您应该只调用一次而不是每次调用 getStaging 时。为您的分期模型尝试这样的事情:
var mongoose = require('mongoose');
var StagingModel = new mongoose.Schema({
user: String,
createdAt: { type: Date, default: Date.now }
});
mongoose.model('CheckStaging', StagingModel);
然后在你的消费代码中,使用
var mongoose = require('mongoose');
require('../lib/staging_manager');
var StagingModel = mongoose.model('CheckStaging');
require 只会执行一次,所以模型应该只用 mongoose 注册一次。
顺便说一句,对于单元测试,mockgoose 是一个出色的模拟 mongoose 的模拟库 - 值得研究!