Search code examples
ruby-on-railsunit-testingruby-on-rails-4fixtures

Debugging rails tests


Testing has been the thing I've avoided until I realized that with well written tests I can avoid having to enter console and running a string of commands over and over again. The big issue I'm having, however, is that when a test fails, I can't see much data to use to debug with. For example, when I'm testing models I need to see the models attributes, and I need to control when it's been built, created, and saved, and a way to detect these states. With rails fixtures and plain vanilla tests this is annoying to do.

How can I make debugging fixtures and rails tests easier? Is there a way to display more console-like information when a test fails? Which testing tools can I use to debug more easily? After all, debugging the tests themselves is a huge waste of time, and the biggest obstacle to using them in the first place.

Thanks!


Solution

  • Debugging your tests is not very different from debugging your code.

    Rails already comes with byebug gem (uncomment it, if necessary):

    group :development, :test do
      gem 'byebug'
    end
    

    Now you are ready to put

    debugger
    

    line anywhere in your code.

    Byebug supports many additional features, which you can find here: https://github.com/deivid-rodriguez/byebug

    Also check https://github.com/deivid-rodriguez/pry-byebug project which provides even better experience for debugging.