我正在编写一个使用具有嵌套对象的mongoose模型的单元测试。我想填充主模型和引用的模型而不调用'填充'并从数据库中获取任何内容。这是coffeescript中的一个例子
CarSchema = new mongoose.Schema
name:
type: String
required: true
engine:
type: ObjectId
ref: 'Engine'
required: true
Car = mongoose.model('Car', CarSchema)
EngineSchema = new mongoose.Schema
name:
type:String
required: true
Engine = mongoose.model('Engine', EngineSchema)
engine1 = new Engine({name: 'test'})
car1 = new Car({engine: engine1, name: 'car'})
assert.equal (car1.engine.name, 'test') #this fails
car1.engine被设置为id而不是引擎对象。 有没有办法让这个工作?
答案 0 :(得分:4)
调用setValue
将保留水合文档:
car1.setValue('engine', engine1)