在Factory Girl中指定belongs_to关联的特征

时间:2014-11-05 02:44:54

标签: ruby-on-rails ruby-on-rails-4 factory-bot

我正在使用Factory Girl,我想指定一个使用特征的belongs_to关联。此代码(没有特征)有效:

FactoryGirl.define do
  factory :challenge_participation do
    user
    challenge
    program
  end
end

但是我如何将with_days特征添加到我的程序中? (with_days特征设置了一些has_many关联。)

我想它会是这样的,但我找不到合适的语法:

FactoryGirl.define do
  factory :challenge_participation do
    user
    challenge
    program :with_days # THIS DOESN'T WORK
  end
end

1 个答案:

答案 0 :(得分:1)

啊,我是个白痴。想出来,program方法接受一个块。所以,你这样做:

FactoryGirl.define do
  factory :challenge_participation do
    user
    challenge
    program { FactoryGirl.create(:program, :with_days) }
  end
end