FactoryGirl has_many协会

Zac*_*tta 5 ruby-on-rails factory-bot

我正在使用FactoryGirl示例来has_many获取http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl的关系.具体来说,例子是:

楷模:

class Article < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :article
end
Run Code Online (Sandbox Code Playgroud)

工厂:

factory :article do
  body 'password'

  factory :article_with_comment do
    after_create do |article|
      create(:comment, article: article)
    end
  end
end

factory :comment do
  body 'Great article!'
end
Run Code Online (Sandbox Code Playgroud)

当我运行相同的示例(当然使用正确的模式)时,会抛出错误

2.0.0p195 :001 > require "factory_girl_rails"
 => true
2.0.0p195 :002 > article = FactoryGirl.create(:article_with_comment)
ArgumentError: wrong number of arguments (3 for 1..2)
Run Code Online (Sandbox Code Playgroud)

有没有新的方法来创建has_many与FactoryGirl关联的模型?

mut*_*amb 3

我认为从那时起 api 已经发生了很大的变化。请参阅此处有关关联的部分以获得进一步的指导:

https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md