Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 708
Allyl Isocyanate Avatar asked Mar 27 '11 01:03

Allyl Isocyanate


People also ask

How do I debug a Ruby test?

First of all you must install ruby-debug. immediately before the line you wish to start debugging. Next step: run your unit tests as you would do normaly. At the moment that ruby reaches the line that contain the debugger directive it will stop and show you a console prompt.

How to debug RSpec in vscode?

To run RSpec:Click left of any line number to set breakpoints, if you like. Click Bug icon on left side of VS Code. Pick configuration from dropdown box in upper left: Debug RSpec - open spec file or.

How do I debug a spec file in Ruby?

Install the ruby-debug gem if you haven't already. But problably you do not need it. Then start rspec with -d parameter to allow debugging and in your code, just add debugger at the point you want to set the breakpoint. That's it.


1 Answers

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.

like image 195
Christopher Maujean Avatar answered Sep 18 '22 08:09

Christopher Maujean