ChatGPT解决这个技术问题 Extra ChatGPT

Running ruby debug in rspec?

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.

I've ended up using Pry, far easier/better. After inserting into Gemfile and installing, throw "binding.pry" anywhere in your spec, execute, and you're rolling. See the screen cast for details.
Pry's a great object explorer, but (even with pry-nav) it just doesn't have the debugger features that the debugger gem has. I wish it did, but I've gone back to debugger.

C
Christopher Maujean

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.


A note here: my solution works for adding ruby debugging to one particular spec file, the solution by zetetic enables debugging for the entire project.
The solution doesn't work for me. I get: cannot load such file -- ruby-debug (LoadError)
Re: the above. Needed to uncomment the Gemfile line with the ruby-debug19 gem. (Put it into :test, :development groups actually.) Works now.
Depends on the GEM You use for debugging. I use "pry-rails" and "pry-byebug" so I would require 'pry-rails'. Also in the Gemfile they need to be included in the test environment
z
zetetic

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

--debug

invalid option: --debug (defined in ./.rspec)
C
Community

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)


+1 and thanks for pointing that out. There is no code modification to enable the debugger, but you do have to modify your test code to invoke the debugger, exactly as shown at the top of the original question (add a debugger statement). Also, if you run your test with spork, the rdb prompt will appear in the spork console, which doesn't echo what you type in. Seems easiest to run without spork.
and if you use byebug, then it becomes -rbyebug
r
rjurado01

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

For some reason, this solution doesn't work. Passes over the test
@Trip that's interesting, maybe instead of byebug try adding debugger?
O
Ode

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!


Huge +1, this is a really great tip
This did not answer the question.
phatmann, this is the most correct way in Rails 3 to allow for logging in rspec tests. Once you have added the method to your spec_helper.rb file you can then log debug statements by using logger.debug "#{1.class}" If you want you can prepend a random string to your debug statement and pipe the output of the tail command to grep in order to filter your debugging statements.
Downvoting. The OP was asking about debugger breakpoints, not logging (which isn't the greatest idea in specs anyway).
Downvoting, for same reason as @MarnenLaibow-Koser. May be useful info, but doesn't answer the OP's actual question.
R
Ryan Taylor

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.