Debugging From Tests With Rails

Sometimes you have a broken test in Rails and you can’t figure out why it’s failing or erroring out.

This is particularly problematic when your attempt to reproduce the failure while interacting with your application does NOT result in errors or problems. This can happen for a variety of reasons, such as incomplete posts to controllers in the tests.

The point is, you have behavior that is broken inside of a test, but you can’t reproduce it outside of that test. Normally you have to stick print lines all of the test to try and figure it out, but as of Rails 2, the breakpointing system can actually be used from within tests (rather than just the server).

Here’s how you can debug from inside of tests.

First, install ruby-debug (gem install ruby-debug)

Next, put the line ‘debugger’ right before the failing line of your test.

Then run your tests. The console running the tests should drop into an interactive debugger. If you want to get to a script/console type environment from there, type ‘irb’. This will make it much easier to figure out where your tests have gone wrong.


Leave a comment