Unit testing 单元测试Mongoose模式有意义吗
出于演示目的,假设我有以下模式+模型:Unit testing 单元测试Mongoose模式有意义吗,unit-testing,mongoose,schema,Unit Testing,Mongoose,Schema,出于演示目的,假设我有以下模式+模型: const UserSchema = mongoose.Schema({ userName: { type: String, required: true, unique: true } }); const UserModel = mongoose.model("User", UserSchema); 我打算有一个大的模式,因此为了便于维护,我在user.schema.js中定义了它,而相应的用户模型在一个单独的文件中
const UserSchema = mongoose.Schema({
userName: {
type: String,
required: true,
unique: true
}
});
const UserModel = mongoose.model("User", UserSchema);
我打算有一个大的模式,因此为了便于维护,我在user.schema.js中定义了它,而相应的用户模型在一个单独的文件中
我用单元测试覆盖了模型,并开始考虑对模式模块进行同样的测试,除了。。。我不确定这是否有道理
我见过人们使用实际的测试数据库连接来测试他们的模型/模式,但这不是一个单元测试,更像是一个集成测试,这是一个不同的概念。对于模式的单元测试,我不关心MongoDB本身是否正常工作
互联网(包括SO)也公布了测试猫鼬验证的代码。这更接近我想要做的,因为基本思想是检查所有预期的道具和限制是否都在模式中。它看起来是这样的:
it('should be invalid if username is empty', function(done) {
new UserModel().validate(function(err) {
expect(err.errors.name).to.exist;
done();
});
});
如果使用实时数据库进行测试,则可以将.validate
更改为.save
然后我突然意识到我会在这里做两件事:
我现在问的问题和你一样:)。你找到答案了吗?遗憾的是,我没有[