Running ruby debug in rspec?

Allyl Isocyanate picture Allyl Isocyanate · Mar 27, 2011 · Viewed 61.6k times · Source

I'm trying to get Ruby debugger running in one of my specs:

describe User do
  it "should be valid" do
    debugger
    User.new.should be_valid
  end
end

When I run rspec though, I get:

debugger statement ignored, use -d or --debug option to enable debugging

I've tried the following:

rake spec --debug
rake spec --debug  --trace
rake spec:models --debug
bundle exec rspec --debug
bundle exec rspec --debug spec/models/
bundle exec rspec --d spec/models/
bundle exec "rspec --debug" spec/models/
bundle exec rspec --debugger spec/models/
bundle exec --debugger rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle exec --debugger rspec spec/models/
bundle exec rspec --debugger spec/models/

Any ideas on how to exec rspec in the right way? I'm on Rails 3.0.5, Ruby 1.9.2, RSpec 2.5.1, ruby-debug19.

Thanks, Justin.

Answer

Christopher Maujean picture Christopher Maujean · Mar 27, 2011

You will get what you want by including require 'ruby-debug' at the top of your spec:

# spec/models/user_spec.rb
require 'spec_helper'
require 'ruby-debug'

describe User do
  it "should be valid" do
    debugger
    User.new.should be_valid
  end
end

You would then run rake spec or rspec as normal

NOTE: I now prefer Ruby 2.0+ and pry. It is pretty much the same process:

require 'spec_helper'
require 'pry-debugger'

describe User do
  it "should be valid" do
    binding.pry
    expect(User.new).to be_valid
  end
end

Also, I generally put requires like this in my spec_helper file, so that pry-debugger is available to all of my specs.