Ruby debug is stepping into rather than stepping over - ruby-on-rails

I am writing a Rails 4 application in Ruby 2, and I am using the debugger gem to debug my code.
Here is the situation currently:
I place a debugger statement in my Rspec tests, run the tests in the shell, and the program breaks as expected. Wishing to step to the next line, I enter n, but the debugger actually steps into the code, showing me the inner workings of libraries I don't care to see.
So the issue is, the debugger command n is acting like s.
How can I solve this problem*?
*I am not willing to put a breakpoint on the next line and then continue, that will get very old, very fast.

Debugger does not yet fully support Ruby 2.0, and one of the issues is that next incorrectly acts like step. You’ll have to wait till the issue is fixed for it to work correctly, or use an alternative debugger such as Byebug.

Related

How do I make a tests pause instead of skipping without using a break point when it fails on a step while running on debug mode - Rubymine cucumber?

I am using Rubymine 6.0 to write cucumber tests using ruby and am very new to it.
When I run a test and a step does not pass , rubymine skips that step and fails the scenario.
This makes perfect sense to me when am running hundreds of tests and I want that to happen the way it is now.
But, How do I make the test pause on that failed step or that particular line of code when running the test in debug mode?
Is there a way around than to put a break point on the step before it fails and then step into it ?
I want the test to pause on that particular point where it failed and let me take over.
Thanks,
RK
All assertions in all testing frameworks that I'm familiar with raise exceptions of certain types when the expectation is not met (or assertion fails) in the test. RubyMine lets you configure the "exception breakpoints" - to have it suspend execution when an exception of a certain type is raised.
I've just tried on RubyMine 6.0.2 with Ruby 2.1 and latest rspec. Rspec raises RSpec::Expectations::ExpectationNotMetError when expectation is not met (according to the rspec's source code), but for some reason RubyMine doesn't stop on that breakpoint - it doesn't work!
Another solution that did work for me was to set a regular breakpoint in the rspec's source code file: rspec-expectations-2.14.0/lib/rspec/expectations/fail_with.rb, in the method "fail_with" - this method is called when any assertion/expectation fails.
I'm sure you could do something like that with Cucumber: just debug into the assertion method's source code and put a breakpoint there.

How to find out why rails server hangs at 100%?

Can someone think of a way to find out where our rails production server hangs? Its CPU is at 99% so I assume it gets lost in a while/for/each loop. Unfortunately we can't find a candidate loop.
The problem does not occur in development and our test suit now has 100% code coverage.
We were already attaching to Ruby via gdb, but didn't know where to go from there.
Any ideas?
Once you have attached with gdb to the busy looping process then call rb_backtrace from gdb:
> call rb_backtrace()
The output from rb_backtrace will be similar to a crash report, and the output will go to your log files similar to a standard Ruby error backtrace.
From there you should hopefully already be a step closer to the solution since you will see where in your Ruby code the process is stuck.
You can check out some more tips here:
http://isotope11.com/blog/getting-a-ruby-backtrace-from-gnu-debugger
This is not a clean solution but at least the following resolved the problem for us: we migrated to the 'thin' webserver and removed 'devise'.

Rails Debugging - Exit controller prematurely

I was wondering how to exit a controller in rails and get the output up to that point.
In PHP I often used the "exit" when debugging to get only the data processed to that point. I haven't found a sollution to this in rails.
If you get a error further down in the code the view is locked from displaying <%= debug %> information.
Some would suggest console or rescue, and I know about these. But isn't there a simpler solution?
In development mode, I often just use puts or awesome_print to print something to the screen that I ran rails server from. That works pretty well for the simple cases.
For anything more complex than that, I use ruby-debug or pry to drop down into an interactive console when it hits the right point.
I have some editor shortcuts to print one of these two snippets:
require 'pry'; binding.pry
require 'ruby-debug'; debugger
Drop these in your code and you can use IRB to inspect (and manipulate) the state of your program.
I highly recommend you give pry a shot. Check it out here:
http://pryrepl.org/
http://railscasts.com/episodes/280-pry-with-rails
There is also the older ruby-debug:
http://railscasts.com/episodes/54-debugging-with-ruby-debug

Rails and Selenium: how to stop/pause execution of a test in the browser?

I'm having a hard (but very interesting time) diving into Behavior Driven Development using Cucumber, RSpec, Selenium, and Rails.
I have my setup ready for testing with Selenium, and it's funny to watch Firefox pop up and run automatically through my scenarios. But one thing I'd like to do is pause or stop execution at a certain point, so I can inspect what Selenium sees at a certain point.
I know of the save_and_open_page command, but this only shows me plain HTML without formatting. So maybe there is a stop_execution method or something that stops Selenium without closing the browser?
Install pry, then put binding.pry in your test where you want it to pause. When you're done, press Ctrl+D or type exit in the REPL that gets opened to continue execution.
or just:
visit '/'
sleep(inspection_time=5)
visit '/dreamland'
All the answers need installing new gems or even setting a sleep which is not the best approach. You can put this line anywhere in you step:
ask "Continue?"
It will stop execution until you enter y (Yes)
So, for example it would look like this:
expect(page).to have_button('Submit')
ask "Continue?"
click_button('Submit')
Use Debugger where you want to stop/pause the execution.
or
In Selenium IDE you can right click on the commands line and you can select Set/Clear Start point to stop/pause the execution.
Okay, I got it working by installing ruby-debug19 (for Ruby 1.9.3), and then just setting a breakpoint somewhere in a Cucumber step.
http://rails.vandenabeele.com/blog/2011/12/21/installing-ruby-debug19-with-ruby-1-dot-9-3-on-rvm/
Another option is to use the Capybara-firebug gem which adds a "Then stop and let me debug" step which basically seems to do the same (I don't know whether it relies on the ruby-debug gems).
try to use selenium.sleep(ms)
this will make the test execution wait for the specified amount of time
You can use do this without installing any gems in rails 5 or above by using byebug. Just type byebug on the line that you want the test to pause at.
Example:
visit post_url(id: posts(:one).id)
byebug
click_on "...", match: :first
Doing this will pause the test after the new page loads but before the next button is clicked. If using an older version of rails you may have to install the byebug gem but it is useful for troubleshooting and I recommend using it regardless.

Ruby / Rails debugging strategy

Please can you share your approach / methodology to debugging in Ruby / Rails.
I'm busy with the Rails tutorial, and am getting the error:
NoMethodError in UsersController#show
undefined method `microposts' for #<User:0x83b43e8>
And that got me thinking about debugging strategies. Does anyone have advice for a new Rails user (and new MVC user) on strategies to approach debugging. What path do you follow? Is there a generally accepted approach? Is there a way to step through the code?
Right now I am using unit testing as a kind of "lint" checker, but that only goes so far.
Although I want to solve it, the actual error I am getting right now is not the main thrust of this question.
(PS: The problem is not a duplicated "show" as documented in elsewhere on Stackoverflow
I haven't seen this mentioned yet but another option is to put a debugger statement in your code. Like this
def some_method
something = 3
debugger
# ... more code
end
If this is in a rails app when the code reaches debugger it will cause the terminal window running the web server to jump into something that looks like an irb session (I'm not exactly sure what it is). From there you can do puts something to see what the value is for example. You can even puts params to see what all the params values are. You can also step through the code, jump to a specific line, etc.
Pry seems to be a better way to go about this but it's how I used to debug before I knew about pry.
Note: You might need to do require 'ruby-debug' if you're doing this outside of rails.
i use a combination of irb, print statements, logging and pry bindings. (pry is a great gem)
irb is a great way to just play around with your ruby or rails app in the console. You could just enter the code from your controller (or similar) and see if it breaks in console for faster feedback loop. But remember you have to do reload! if you change anything in your class/module.
print statements are easy if you're running tests and just want it to output something a different points in your test. But if your testing in a browser I would recommend writing to the logger: Rails.logger.debug "...". But remember to set your logging level in your configuration to DEBUG -or- you can just do Rails.logger.info instead which should show up by default. Then you can just tail or view the logs in my_app/logs/development.rb.
My favorite method for really tricky bugs is that if the error is happening in a test you can just place binding.pry in the preceding line and then it will pause your test at that line and drop you into a console. I recommend watching the rails casts for more in-depth info: http://railscasts.com/episodes/280-pry-with-rails
I do not start Rails project without 'pry' gem.
Add gem to Genfile:
group :development, :test do
gem 'pry'
end
and stop request execution anywhere in your project, just put
binding.pry
to your model, controller, tests ..., or
<% binding.pry %>
in your view's, templates, partials.
Then you can check what ever you want objects, params, variables ...
Type exit to leave pry environment, and request will continue.
The Ruby on Rails Guide would be a great place to start, but there's plenty more.
I always have a rails console session or at minimum an irb session to play with to see if things do what I think they do.
I also use RubyMine which has an excellent integrated debugger http://www.jetbrains.com/ruby/
Beside pry gem, another option would be byebug. This gem enables you to temporarily stop code execution at a breakpoint, which is marked with keyword byebug inside the code. When execution reaches the breakpoint, a marker will be pointing to the current line, and you are able to type in commands.
This Gem offers a huge set of commands, the most commonly used ones are:
next - this command enables you to go to next line
step - goes into each invoked method step by step
break - it stops the execution of code
continue - continues code execution
This is a great article to check for debugging in rails.

Resources