问题
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.
回答1:
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.
回答2:
You can create an .rspec
configuration file in the root of your project and include the line:
--debug
回答3:
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)
回答4:
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
回答5:
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!
回答6:
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.
来源:https://stackoverflow.com/questions/5446594/running-ruby-debug-in-rspec