【问题标题】:Generating test data with Faker使用 Faker 生成测试数据
【发布时间】:2023-03-30 17:50:01
【问题描述】:

我正在编写一个使用 faker 和 JSON-Schema-Faker 包生成测试数据的脚本。

寻找具有“模式继承”和可选字段的示例。 例如,我有一个带有必填字段的“用户”对象:“_id”、“名字”、“姓氏”、“用户名”、“电子邮件”和一个 [并且只有一个]:customerProfile、partnerProfile 和 adminProfile。每个可选字段(如果存在):保留对应于另一个模式的非空值数组。

请指点我相关的例子。

【问题讨论】:

  • 如果它们都是“配置文件”并且始终包含一个实体,为什么不将其引用为 profile 属性而不是每个属性中的每个属性?在我看来,这将简化您的架构,并意味着减少对空值/空值的检查。这样,其中包含的架构会告诉您它是什么类型的配置文件。只是一个想法,我可能错过了一些东西。

标签: javascript node.js faker feathersjs


【解决方案1】:

结局:

admin: function(first, last){
  return {
    "displayUsername": first + " " + last,
    "active": true
  };
},
...

var profileIndex = Math.round(Math.random() * 2);
var profileTypes = ["admin", "customer", "partner"];
var currentProfileType = profileTypes[profileIndex];
var userRecord = {
  "username": username, 
  "firstName": firstName, 
  "lastName": lastName, 
  "email": email, 
  "_id": _id
  //(ES6 Syntax) ,[currentProfileType + "Profile"]: profileBuilders[currentProfileType](firstName, lastName);
};
userRecord[currentProfileType + "Profile"] = profileBuilders[currentProfileType](firstName, lastName);

return userRecord;

【讨论】:

    猜你喜欢
    • 2019-04-29
    • 2018-07-08
    • 2020-07-22
    • 2018-09-08
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    相关资源
    最近更新 更多