Undefined method after_create with FactoryGirl

不想你离开。 提交于 2019-12-10 02:28:29

问题


I'm trying to defined a has_many relationship in FactoryGirl using the after_create callback, like so in /spec/factories/emails.rb:

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

The attachment is defined in a seperate factory /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

Using the :attachment in my specs works absolutely fine, so I'm confident that the factory for that is not the problem, however when I try and create an :email from the factory I get the following exception thrown:

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

I'm at a bit of a loss as to what to do, can't seem to find any one else getting the same error.


回答1:


FactoryGirl recently changed the syntax for callbacks. I think the following will work:

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


来源:https://stackoverflow.com/questions/15003968/undefined-method-after-create-with-factorygirl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!