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

Pavel Feldman picture Pavel Feldman · Feb 26, 2010 · Viewed 9.4k times · Source

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

Answer

Plicatibu picture Plicatibu · Jul 16, 2010

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.