简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM