我正在尝试使用mocha的--watch
选项。它工作正常,直到我必须用猫鼬模型做任何事情。显然,根据我的理解,Mongoose保留了某种缓存,我得到的错误是tracked and closed。问题是,我对这整个事情有点新意,需要一些指导如何以及在哪里放置我需要的东西来实现这一点。所以,我试过了:
mongoose.model
。工作,但显然违背了--watch
。mongoose.disconnect
)断开连接。--watch
并每次都运行新的测试。在这三个中,很明显在第三个作品中,我真的想要使用我的构建工具的所有功能。所以,这就是我所拥有的。我哪里错了?
var mongoose = require('mongoose'),
register = require('./_register');
var userSchema = mongoose.Schema({
email: String,
password: String
});
userSchema.methods.setPassword = function(password) {
this.password = password;
};
module.exports = mongoose.model('User', userSchema);
var User = require('../models/user');
describe('User', function() {
describe('#setPassword()', function() {
it('should set the password', function() {
var user = new User();
user.setPassword('test');
user.password.should.not.equal('');
});
it('should not be in plaintext');
});
describe('#verifyPassword()', function() {
it('should return true for a valid password');
it('should return false for an invalid password');
});
});
答案 0 :(得分:11)
我在afterEach()块中运行它取得了一些成功:
delete mongoose.models.YourModel;
delete mongoose.modelSchemas.YourModel;
答案 1 :(得分:0)
为了补充boneskull,这里是我使用的ES6代码,包括连接打开/关闭以测试User
mangoose模型:
import mongoose from 'mongoose'
import User from '/models/user'
describe('User model', () => {
beforeEach((done) => {
mongoose.connect('mongodb://localhost/somedatabase-test', done)
})
afterEach((done) => {
delete mongoose.models.User
delete mongoose.modelSchemas.User
mongoose.connection.close(done)
})
...
})