Running ruby debug in rspec?

Ruby on-RailsRspecRakeRuby Debug

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

You can create an .rspec configuration file in the root of your project and include the line:

--debug

Solution 3 - Ruby on-Rails

For Ruby >= 1.9.2

You should install the debugger gem instead of ruby-debug19. It you use bundler, you just put this in your Gemfile:

group :test do
  gem "debugger"
end

After that you can just put

rspec < 3.0
--debug
rspec >= 3.0
-rdebugger

in your .rspec file

Then you can just run

bundle exec rake spec

without any additional arguments. There is no need to modify your source code either (not even your test source code)

Solution 4 - Ruby on-Rails

For ruby 2.0 I use byebug: https://github.com/deivid-rodriguez/byebug

gem 'byebug'

Code:

# spec/models/user_spec.rb
require 'spec_helper'
require 'byebug'

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

Solution 5 - Ruby on-Rails

The best way I have found to debug in rSpec is by adding the following to your 'spec_helper.rb' file

def logger
  Rails.logger
end

You can then access all the logger methods within your rSpec files and incorporate such things as tagged logging. This of course is for Rails 3 and up. If you have anything prior to Rails 3 then add this instead:

def logger
  RAILS_DEFAULT_LOGGER
end

Once you have your logging statements in place you can enter

tail -f log/test.log

in your terminal shell in order to watch your logging statements while the tests are run.

Of course in your actual rspec test you would enter something such as

logger.debug "#{1.class}"  # => Fixnum

If you want to filter your debug statements from the rest of your test log simply prepend a random string on to your debug statement and pipe the output of the tail command to grep.

Example:

logger.debug "random_string #{1.class}"   # => Fixnum

tail -f log/test.log | grep random_string
Update

I've changed my opinion on this. You should install pry, pry-doc, and pry-debug, pry-debugger, and pry-rails. Then use binding.pry in your code to open an interactive debugger console that rules the world!

Solution 6 - Ruby on-Rails

The best and cleanest option is to use --require in your .rspec file. What you put depends on which gem you use for debugging.

--color
--require pry
--require rails_helper

These correspond to command line options (-d or --debug is now deprecated).

Feel free to use debugger, ruby-debug or pry (pry-rails in your Gemfile).

For your Gemfile:

group :test, :development do
  gem 'pry-rails'
end

Putting require 'ruby-debug' etc. at the top of your spec is simply more tightly coupled -- especially since here the top voted comment suggests putting it individually in ALL your files. With the new .rspec file you shouldn't need to put require 'spec_helper' or require 'rails_helper' at the top of your files anymore.

They make more sense as implicit command line arguments.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAllyl IsocyanateView Question on Stackoverflow
Solution 1 - Ruby on-RailsChristopher MaujeanView Answer on Stackoverflow
Solution 2 - Ruby on-RailszeteticView Answer on Stackoverflow
Solution 3 - Ruby on-RailsJarlView Answer on Stackoverflow
Solution 4 - Ruby on-RailsdrinorView Answer on Stackoverflow
Solution 5 - Ruby on-RailsOdeView Answer on Stackoverflow
Solution 6 - Ruby on-RailsRyan TaylorView Answer on Stackoverflow