我正在尝试使用FactoryGirl创建虚拟数据。
用户有很多帖子,帖子有很多视频,视频有很多评论。 评论属于视频和用户。 视频属于帖子和用户。 帖子属于用户。
我想创建至少20个用户,每个用户至少有10个帖子,每个帖子至少有1个视频,每个视频至少有1个评论。
到目前为止,我有以下工厂,但我似乎无法让视频或评论工作。
FactoryGirl.define do
factory :comment do
sequence(:body) { |n| "#{n}body" }
video
user
end
end
FactoryGirl.define do
factory :post do
sequence(:title) { |n| "#{n}title" }
date Date.today.strftime('%m/%d/%Y')
body Faker::Lorem.paragraph(3)
tags Faker::Lorem.words(4)
user
trait :with_videos do
after(:build) do |post|
create(:video, post: post)
end
end
end
end
FactoryGirl.define do
factory :user do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
sequence(:username) { |n| "#{n}username" }
sequence(:email) { |n| "#{n}email@website.com" }
phone Faker::PhoneNumber.phone_number
password Faker::Internet.password(6, 20)
country Faker::Address.country
state Faker::Address.state_abbr
city Faker::Address.city
zip Faker::Address.zip
seeking_coach true
accept_email true
accept_phone true
end
end
FactoryGirl.define do
factory :video do
sequence(:title) { |n| "#{n}title" }
sequence(:url) { |n| "https://www.youtube.com/watch?v=tYm_#{n}2oCVdSM" }
embed_id { "#{url}.split('=').last" }
post
user
trait :with_comments do
after(:build) do |video|
create(:comment, video: video)
end
end
end
end
答案 0 :(得分:0)
我认为在工厂中,钩子应该是after(:create)
而不是after(:build)
,例如:
after(:create) do |video|
create(:comment, video: video)
end
以下是所有更新的工厂:
<强>规格/工厂/ users.rb的强>
FactoryGirl.define do
factory :user do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
sequence(:username) { |n| "#{n}username" }
sequence(:email) { |n| "#{n}email@website.com" }
phone Faker::PhoneNumber.phone_number
password Faker::Internet.password(6, 20)
country Faker::Address.country
state Faker::Address.state_abbr
city Faker::Address.city
zip Faker::Address.zip
seeking_coach true
accept_email true
accept_phone true
trait :with_10_posts do
after(:create) do |user|
create_list(:post, 10, :with_videos, user: user)
end
end
end
end
<强>规格/工厂/ posts.rb 强>
FactoryGirl.define do
factory :post do
sequence(:title) { |n| "#{n}title" }
date Date.today.strftime('%m/%d/%Y')
body Faker::Lorem.paragraph(3)
tags Faker::Lorem.words(4)
user
trait :with_videos do
after(:create) do |post|
create(:video, :with_comments, post: post)
end
end
end
end
<强>规格/工厂/ videos.rb 强>
FactoryGirl.define do
factory :video do
sequence(:title) { |n| "#{n}title" }
sequence(:url) { |n| "https://www.youtube.com/watch?v=tYm_#{n}2oCVdSM" }
post
trait :with_comments do
after(:create) do |video|
create(:comment, video: video)
end
end
end
end
<强>规格/工厂/ comments.rb 强>
FactoryGirl.define do
factory :comment do
sequence(:body) { |n| "#{n}body" }
video
end
end
并通过RSpec测试确保工厂设置正常运行:
describe 'Factories' do
it 'creates 20 users, each with at least 10 posts, each post with at least 1 video, each video with at least 1 comment' do
FactoryGirl.create_list(:user, 20, :with_10_posts)
expect(User.count).to eq(20)
user = User.take
expect(user.posts.count).to be >= 10
post = user.posts.take
expect(post.videos.count).to be >= 1
video = post.videos.take
expect(video.comments.count).to be >= 1
end
end
以下是所有代码 - https://github.com/shhavel/stackoverflow_question_40136020