使用FactoryGirl的未定义方法after_create

Sir*_*ins 11 rspec ruby-on-rails-3 factory-bot

我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)

附件在单独的工厂/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
Run Code Online (Sandbox Code Playgroud)

在我的规范中使用:附件工作非常好,所以我确信工厂不是问题,但是当我尝试创建一个:来自工厂的电子邮件时,我得到以下异常抛出:

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

我有点不知所措,似乎找不到任何其他人得到同样的错误.

Dhr*_*ruv 31

FactoryGirl 最近改变了回调的语法.我认为以下内容可行:

FactoryGirl.define do
  factory :email do
    after(:create) do |email|
      email.attachments << FactoryGirl.build(:attachment)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)