あなたは以下のようにafter(:build)
とafter(:create)
コールバックを使用することができます。
FactoryGirl.define do
factory :option do
transient do
parent_option nil
no_of_suboptions nil
end
name Faker::Internet.name
after(:build) do |option, evaluator|
#option.activity_id = evaluator.activity.id
if not evaluator.parent_option.blank?
#option.parent_id = evaluator.parent_option.id
option.parent = evaluator.parent_option
end
end
factory :option_with_suboptions do
after(:create) do |option, evaluator|
create_list(:option, evaluator.no_of_suboptions, :activity => option.activity, :parent => option)
end
end
end
end
使用
FactoryGirl.create(:option_with_suboptions, :activity => activity, :no_of_suboptions => 5)
一つはactivity
オブジェクトが存在することを確認する必要があります。一つは、あなたのGemfileにfaker
宝石を追加Gemfileno_of_suboptions
を使用して作成することが
をsuboptions
の数を設定することができます。
group :development, :test do
gem 'faker'
end
これは「name Faker :: Internet.name'」とは何ですか? –
申し訳ありません。 'Faker :: Internet.name'はあなたのために偽のデータを生成します。 gemfileにgem 'faker'を追加して' bundle'を実行することができます。 – fossil