joshua.paling
joshua.paling

Reputation: 13952

Specify trait for belongs_to assocation in Factory Girl

I'm using Factory Girl, and I want to specify a belongs_to association that uses a trait. This code (without traits) works:

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

But how would I add the with_days trait to my program? (The with_days trait sets up some has_many associations).

I imagine it'd be something like this, but I can't find the right syntax:

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

Upvotes: 2

Views: 530

Answers (1)

joshua.paling
joshua.paling

Reputation: 13952

Ah, I'm an idiot. Figured it out, the program method accepts a block. So, you do it like this:

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

Upvotes: 2

Related Questions