我正在尝试使用after_create回调在FactoryGirl中定义一个has_many关系,就像在/spec/factories/emails.rb中一样:
FactoryGirl.define do
factory :email do
after_create do |email|
email.attachments << FactoryGirl.build(:attachment)
end
end
end
附件在单独的工厂/spec/factories/attachment.rb中定义:
FactoryGirl.define do
factory :attachment do
# Attach the file to paperclip
file { fixture_file_upload(Rails.root.join('spec', 'support', 'myimage.png'), 'image/png') }
end
end
在我的规范中使用:附件工作非常好,所以我确信工厂不是问题,但是当我尝试创建一个:来自工厂的电子邮件时,我会抛出以下异常:
Failure/Error: email = FactoryGirl.create(:email)
NoMethodError:
undefined method `after_create=' for #<Email:0x007ff0943eb8e0>
我对于做什么感到有点失落,似乎无法找到任何其他人得到同样的错误。
答案 0 :(得分:30)
FactoryGirl recently changed回调的语法。我认为以下内容可行:
FactoryGirl.define do
factory :email do
after(:create) do |email|
email.attachments << FactoryGirl.build(:attachment)
end
end
end