Is it possible to debug unit tests in Ruby on Rails? Is it possible to debug unit tests in Ruby on Rails? ruby-on-rails ruby-on-rails

Is it possible to debug unit tests in Ruby on Rails?


Yes, it is possible.

First of all you must install ruby-debug.

sudo gem install ruby-debug

then you write

debugger

immediately before the line you wish to start debugging.

Next step: run your unit tests as you would do normaly.

At the moment that ruby reaches the line that contain the debugger directive it will stop and show you a console prompt.

Now you can debug in the same way as you would do with gdb.

This link explains it in a very nice way: http://blog.nanorails.com/articles/2006/07/14/a-better-rails-debugger-ruby-debug

I hope this informtaion will be useful to you.


As a (late) update, you could also use byebug which is targeting Ruby 2 debugging.

It's also included by default in the Gemfile that comes with any new Rails project and there is also a nice tutorial in the Debugging Rails Applications Guide.

You can just insert

byebug

anywhere in your test file and then run the test as you normally would.