【问题标题】:Mongoose create test data and populate without a database connectionMongoose 创建测试数据并在没有数据库连接的情况下填充
【发布时间】:2014-12-20 20:13:59
【问题描述】:

我正在编写一个使用具有嵌套对象的猫鼬模型的单元测试。我想填充主模型和引用模型而不调用“填充”并从数据库中获取任何内容。这是咖啡脚本中的一个例子

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 而不是引擎对象。 有没有办法让它工作?

【问题讨论】:

    标签: javascript node.js unit-testing coffeescript mongoose


    【解决方案1】:

    调用setValue 将保留水合文档:

    car1.setValue('engine', engine1)
    

    【讨论】:

    • 希望我能给你超过 1 个支持 - 谢谢@Andrew
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2018-05-21
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多