我在猫鼬中创建了一个用户(注册)模型。该模型可从 mocha 中创建的测试用例以及前端的注册表单中访问。新用户可以通过前端从注册表中保存,但是用mocha编写的测试用例不能保存。
当我检查代码时,测试用例上connection.readyState
的猫鼬是2
( )和用户注册表单上的( )。connecting
1
connected
模型/users.js
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;
如何纠正connection.readyState
测试用例中的猫鼬?