[英]RSpec/FactoryGirl Stack level too deep
I'm having trouble with what looks like a very simple problem. 我遇到了一个看似非常简单问题的问题。
So I have 3 models: Foo, Bar, and Lol 所以我有3个型号:Foo,Bar和Lol
Here's the code that's giving me errors. 这是给我错误的代码。
spec/factories/foos.rb 规格/工厂/ foos.rb
FactoryGirl.define do
factory :foo do
after(:create) do |foo|
create_list(:bar, 1, foo: foo)
end
end
end
spec/factories/bars.rb 规格/工厂/ bars.rb
FactoryGirl.define do
factory :bar do
foo
lol
end
end
spec/factories/lols.rb 规格/工厂/ lols.rb
FactoryGirl.define do
factory :lol do
after(:create) do |lol|
create_list(:bar, 1, lol: lol)
end
end
end
I'm trying to get this test to pass 我正试图让这个测试通过
spec/models/foo_spec.rb 规格/型号/ foo_spec.rb
require 'spec_helper'
describe Foo do
it "works" do
foo = FactoryGirl.create(:foo)
puts foo.bars
foo.bars.each {|bar| puts bar.lol}
foo.should_not be_nil
end
end
You have a lot of recursion going on with your current factories setup which is causing the Stack level too deep
error. 你当前的工厂设置有很多递归,这导致
Stack level too deep
错误。
In short, when you call foo = FactoryGirl.create(:foo)
简而言之,当你调用
foo = FactoryGirl.create(:foo)
factory :foo => create_list(:bar, 1, foo: foo) => factory :bar => factory :foo => create_list(:bar, 1, foo: foo) ...... keeps looping
factory:foo => create_list(:bar,1,foo:foo)=> factory:bar => factory:foo => create_list(:bar,1,foo:foo)......保持循环
Same for lol
. lol
。
You would need to refactor your code. 您需要重构代码。 One possible solution is as below:
一种可能的解决方案如下:
Change your factories as: 将您的工厂更改为:
## spec/factories/foos.rb
FactoryGirl.define do
factory :foo do
end
end
## spec/factories/bars.rb
FactoryGirl.define do
factory :bar do
foo
lol
end
end
## spec/factories/lols.rb
FactoryGirl.define do
factory :lol do
end
end
Update your example as below: 更新您的示例如下:
## spec/models/foo_spec.rb
require 'spec_helper'
describe Foo do
it "works" do
foo = FactoryGirl.create(:bar).foo
puts foo.bars
foo.bars.each {|bar| puts bar.lol}
foo.should_not be_nil
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.