简体   繁体   中英

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. Thanks to Frederick Cheung, whose comment suggested this approach.

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 "Attendee#next_page" do 
  include Rails.application.routes.url_helpers
end

I have a lot to learn about 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. 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

In this example pay attention to var observation which is not initialised in the wrong snippet

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)

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(: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. 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.

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!

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