Associations
factories/users.rb
FactoryGirl.define do
factory :user, aliases: [:author] do
name 'murajun1978'
password 'murajun1978'
password_confirmation { |u| u.password }
factory :user_posts do
ignore do ←これ
count 1
end
after(:create) do |user, value|
FactoryGirl.create_list(:post, value.count, author: user)
end
end
end
factory :post do
author ←ここ
sequence(:title) {|n| "text#{n}"}
end
end
factories/post.rb
factory :post do
association :author, factory: :user ←ここ
sequence(:title) {|n| "text#{n}"}
end
でも、このままだとbuildしたときにuserオブジェクトが作られない
factories/post.rb
factory :post do
association :author, factory: :user, strategy: :build ←ここ
sequence(:title) {|n| "text#{n}"}
end
strategy: :buildオプションを指定するとbuildだけでuserオブジェクトも一緒に作成されるよ
明日は継承について書こうかなヘ(^o^)ノ
FactoryGirlしりーずとか書いてるけどFactoryGirlのGetting Startedにすべてがあるので…
そっちを見るのがはやいかも(;・∀・)
0 件のコメント:
コメントを投稿