exAspArk
exAspArk

Reputation: 113

Rspec and FactoryGirl: SystemStackError: stack level too deep

I have a problem with FactoryGirl:

Here is my 2 factories:

FactoryGirl.define do
  factory :task do
    ...
    after(:build) do |task|
      question = FactoryGirl.create(:question)
      task.questions = [question]
    end
  end
end

and

FactoryGirl.define do
  factory :question do
    association :task, factory: :task
    ...
  end
end

Question factory creates Task, Task factory creates Question, etc. So, I have a message: "SystemStackError: stack level too deep".

How can I solve this problem without breaking the associations?

Upvotes: 3

Views: 3517

Answers (1)

Chris Salzberg
Chris Salzberg

Reputation: 27374

You're getting a "stack level too deep" error because you're defining both factories in terms of each other. You don't need the association :task, factory: task line in the question factory -- the association will be set when you create a task.

Try this for your task factory:

FactoryGirl.define do
  factory :task do
    ...
    questions { [ FactoryGirl.create(:question) ] }
  end
end

Upvotes: 7

Related Questions