我有广告工厂,但如何将其分配给user.ads属性?我试过了
@ad = user.ads.build(:ad)
但这对我不起作用
FactoryGirl.define do
factory :user do
name "My"
email "my@example.com"
end
factory :ad do
title "Ola"
description "Hello"
user
end
end
let(:user) { FactoryGirl.create(:user) }
#so i don't type it like this:
@ad = user.ads.build(title: "Lorem ipsum",description: "Lorem ipsum")
subject { @ad }
答案 0 :(得分:1)
let(:user) { FactoryGirl.create(:user) }
5.times { FactoryGirl.create(:ad, user: user) }
user.ads #=> array of ads
当然,只有在适当地设置User
和Ad
模型中的关联时,它才会起作用。