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.
You will get what you want by including
require 'ruby-debug'
at the top of your spec:You would then run
rake spec
orrspec
as normalNOTE: I now prefer Ruby 2.0+ and pry. It is pretty much the same process:
Also, I generally put requires like this in my spec_helper file, so that pry-debugger is available to all of my specs.
For ruby 2.0 I use byebug: https://github.com/deivid-rodriguez/byebug
Code:
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:
After that you can just put
rspec < 3.0
rspec >= 3.0
in your
.rspec
fileThen you can just run
without any additional arguments. There is no need to modify your source code either (not even your test source code)
The best way I have found to debug in rSpec is by adding the following to your 'spec_helper.rb' file
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:
Once you have your logging statements in place you can enter
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
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:
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!
The best and cleanest option is to use
--require
in your.rspec
file. What you put depends on which gem you use for debugging.These correspond to command line options (-d or --debug is now deprecated).
Feel free to use
debugger
,ruby-debug
orpry
(pry-rails in your Gemfile).For your Gemfile:
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 putrequire 'spec_helper'
orrequire 'rails_helper'
at the top of your files anymore.They make more sense as implicit command line arguments.
You can create an
.rspec
configuration file in the root of your project and include the line: