gpt4 book ai didi

ruby-on-rails-3 - FactoryGirl 的未定义方法 after_create

转载 作者:行者123 更新时间:2023-12-02 23:44:49 25 4
gpt4 key购买 nike

我尝试使用 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

在我的规范中使用 :attachment 工作得非常好,所以我相信工厂不是问题,但是当我尝试从工厂创建 :email 时,我会抛出以下异常:

Failure/Error: email = FactoryGirl.create(:email)
NoMethodError:
undefined method `after_create=' for #<Email:0x007ff0943eb8e0>

我有点不知所措,似乎找不到其他人遇到同样的错误。

最佳答案

工厂女孩 recently changed回调的语法。我认为以下方法会起作用:

FactoryGirl.define do
factory :email do
after(:create) do |email|
email.attachments << FactoryGirl.build(:attachment)
end
end
end

关于ruby-on-rails-3 - FactoryGirl 的未定义方法 after_create,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15003968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com