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

So, I just moved to new project that is built on Ruby on Rails. I'm new to it and I'm still learning it. I can debug most of the application code, but it seems I can not debug unit test code and code that is tested.

When I run unit testing in debug mode, I can set a breakpoint in unit test class code, for example where fixtures are added, but breakpoints do not work inside test method code and do not work inside tested code, where they do work during normal workflow. Everyone else in team uses print statement for unit test debugging.

So, is that even possible to debug unit test code in rails?

We are using jruby 1.3.1, rails-2.3.1 and I use IntelliJ IDEA to debug, also I'm ok if I have to switch to eclipse or netbeans.

EDIT: actually, "no, it is not possible" would be a helpful answer as well, if you know it is so. At least I would stop wasting time on it

Answers


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.


Need Your Help

How to create a Youtube style loading bar with Rails 4's Turbolinks?

ruby-on-rails ajax progress-bar ruby-on-rails-4 turbolinks

Is there a way to make a Youtube style loading bar (the inter-page across the top bar) with turbo-links.