从猫鼬模型生成测试数据

时间:2016-09-21 19:02:55

标签: node.js mongodb mongoose

我在nodejs app

中有这个模型
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var ServiceSchema = new Schema({
    Taxy: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Taxy',
        required: 'Taxy cannot be blank'
    },
    User: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: 'User cannot be blank'
    },
    Servicio: String,
    Minutos: Number,
    Costo: Number, 
    Desc: String,
    From: String, 
    To: String,
    created: {
        type: Date,
        default: Date.now
    }
});

mongoose.model('Service', ServiceSchema);

我已经浏览了几天而且我还没有找到一个从模型服务生成随机数据的框架,比如

var Service = require('mongoose').model('Admin').schema;
var jsondata = generateRandomeFromModel(Service)

2 个答案:

答案 0 :(得分:1)

您可以使用mongoose-fakery模块生成test数据

示例:

var mongoose = require('mongoose')
  , Schema = mongoose.Schema;

var UserSchema = new Schema({
    name: String,
    surname: String
});

mongoose.model('User', UserSchema);
In your tests or fixture files:

var fakery = require('mongoose-fakery');

fakery.fake('user', mongoose.model('User'), {
    name: 'john',
    surname: 'doe'
});

您可以使用完整参考here

答案 1 :(得分:0)

我找到了两年前创建的可行解决方案。我也对其进行了测试。

https://www.npmjs.com/package/mongoose-dummy