简体   繁体   English

Rspec和FactoryGirl:SystemStackError:堆栈级别太深

[英]Rspec and FactoryGirl: SystemStackError: stack level too deep

I have a problem with FactoryGirl: 我对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". 问题工厂创建任务,任务工厂创建问题,等等。因此,我收到一条消息:“ SystemStackError:堆栈级别太深”。

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. 您在question工厂中不需要association :task, factory: task行-创建任务时将设置关联。

Try this for your task factory: 为您的task工厂尝试以下task

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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