可能是一个基本问题,但我找不到答案。我有一个配置文件模型,配置文件可以从预先制作的列表中获取几个标签。我想使用工厂女孩创建不同的配置文件,每个配置文件都有不同的标签。这是我目前的代码:
factory :profile do
sequence(:user_id) { |n| Student.find(n).id }
sequence(:title) { |n| "title #{n}" }
sequence(:category) { |n| "category #{n}"}
rate 8
zip_code 11024
description "Lorem ipsum timeLorem ipsum timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum timeLoem ipsum time"
state "ny"
tag_list ["php", "python"]
end
我有十个标签,我希望Factory Girl创建多个配置文件,所有配置文件都包含不同的标签组合。我该怎么做?
答案 0 :(得分:0)
如果您希望在创建配置文件时随机选择标记,请执行此操作
factory :profile do
sequence(:user_id) { |n| Student.find(n).id }
sequence(:title) { |n| "title #{n}" }
sequence(:category) { |n| "category #{n}"}
rate 8
zip_code 11024
description "Lorem ipsum timeLorem ipsum timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum timeLoem ipsum time"
state "ny"
tag_list ["php", "python", "a"..].sample(2)
end
FactoryGirl.create_list(:profile, 10) #this will create 10 profiles with random tags
如果您想控制添加的标记,请使用traits
factory :profile do
sequence(:user_id) { |n| Student.find(n).id }
sequence(:title) { |n| "title #{n}" }
sequence(:category) { |n| "category #{n}"}
rate 8
zip_code 11024
description "Lorem ipsum timeLorem ipsum timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum t timeLorem ipsum timeLoem ipsum time"
state "ny"
tag_list ["php", "python", "a"..].sample(2)
trait :tag_1 do
tag_list [..]
end
trait :tag_2 do
tag_list [..]
end
end