简体   繁体   English

rspec堆栈级别太深

[英]rspec stack level too deep

When I run my model specs and controller specs separately, it's fine. 当我单独运行我的模型规格和控制器规格时,它很好。 When I run them together, I get a stack overflow, literally :) 当我一起运行它们时,我得到一个堆栈溢出,字面意思:)

$ bundle exec rspec --fail-fast spec/models
........

Finished in 0.44274 seconds
8 examples, 0 failures

$ bundle exec rspec --fail-fast spec/controllers
..

Finished in 0.99339 seconds
2 examples, 0 failures

$ bundle exec rspec --fail-fast spec
F

Failures:

  1) HerpController derp derp example
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /Users/jared/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/layouts.rb:359

Finished in 0.02241 seconds
1 example, 1 failure

How do I even begin to debug this? 我怎么开始调试这个? Thanks. 谢谢。

Removing half of my specs at a time turned up the problem. 一次删除一半我的规格出现了问题。 I suppose this is an example of bisect debugging. 我想这是bisect调试的一个例子。 Thanks to Frederick Cheung, whose comment suggested this approach. 感谢Frederick Cheung,他的评论提出了这种方法。

For posterity, this was the problem. 对后人来说,这就是问题所在。

include Rails.application.routes.url_helpers
describe "Attendee#next_page" do
end

Apparently, includes go inside the describe 显然,包括进入describe

describe "Attendee#next_page" do 
  include Rails.application.routes.url_helpers
end

I have a lot to learn about rspec. 我有很多关于rspec的知识。 :) :)

You can either put a debugger statement in your code and debug that way, or just start using puts "got here" in the places of your code that you know are being run. 您可以在代码中放置一个debugger语句并以这种方式进行调试,或者只是在您知道正在运行的代码的位置开始使用puts "got here" I would suggest using something meaningful instead of "got here" too :-) 我建议使用有意义的东西,而不是“到这里”:-)

Possibly you can get "Unable to find matching line from backtrace" error in case you are checking some var which wasn't initialised actually 可能你可以得到“无法找到来自backtrace的匹配行”错误,以防你检查一些实际上没有初始化的var

In this example pay attention to var observation which is not initialised in the wrong snippet 在此示例中,请注意未在错误的代码段中初始化的var 观察

wrong snippet 错误的片段

describe "GET index" do
  it "assigns all observations as @observations" do
    get :index, {}, valid_session
    assigns(:observations).should eq([observation])
  end
end

fixed example (line 3) 固定的例子 (第3行)

describe "GET index" do
  it "assigns all observations as @observations" do
    observation = Observation.create! valid_attributes
    get :index, {}, valid_session
    assigns(:observations).should eq([observation])
  end
end

Sometimes we rely on using let as initializer, but forget to add it eg 有时我们依赖于使用let作为初始化程序,但忘记添加它,例如

let(:observation) {FactoryGirl.create(:observation)}

I'd start either with puts or raise statements at key points in your code so you can start to narrow down which line is causing the problem. 我会在代码的关键点开始使用putsraise语句,这样你就可以开始缩小导致问题的那一行。 Once you start getting close, you can comment out one line at a time to figure out which line the problem disappears with - as soon as you've got it down to one line, you can figure out what that line is doing that Ruby doesn't like. 一旦你开始接近,你可以一次注释掉一行来找出问题消失的那一行 - 一旦你把它归结为一行,你就可以弄清楚那行是做什么的Ruby没有不喜欢。

In general terms of where to start, "Stack level too deep" is usually an infinite loop or infinite recursion problem - I'd imagine that there's something going on where a model is invoking a controller that's invoking the model, or vice-versa. 一般来说,从哪里开始,“堆栈级别太深”通常是无限循环或无限递归问题 - 我想象有一些模型正在调用调用模型的控制器,反之亦然。 No way to know for sure until you start commenting out lines, but any place where you've got function calls is going to belong on your suspect short list. 在你开始评论线路之前无法确切知道,但任何有功能调用的地方都属于你的可疑短名单。 Good luck! 祝好运!

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

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