I am writing a unit test that uses a mongoose model which has nested object. I want to populate the main model and the referenced model without calling 'populate' and fetching anything from the database. Here is an example in 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
What happens is that car1.engine is set to an id and not to the engine object. Is there a way to get this working?
calling setValue
will retain the hydrated document:
car1.setValue('engine', engine1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With