Start ruby debugger if rspec test fails Start ruby debugger if rspec test fails ruby-on-rails ruby-on-rails

Start ruby debugger if rspec test fails


Use pry-rescue, it's the spiritual successor to plymouth:

From the Readme:

If you're using RSpec or respec, you can open a pry session on every test failure using rescue rspec or rescue respec:

$ rescue rspecFrom: /home/conrad/0/ruby/pry-rescue/examples/example_spec.rb @ line 9 :     6:     7: describe "Float" do     8:   it "should be able to add" do =>  9:     (0.1 + 0.2).should == 0.3    10:   end    11: endRSpec::Expectations::ExpectationNotMetError: expected: 0.3     got: 0.30000000000000004 (using ==)[1] pry(main)>


You won't get access to local variables (easily) without debugger being in scope of the block, however RSpec provides you with around hooks which let's you do this:

config.around(:each) do |example|  result = example.run  debugger if result.is_a?(Exception)  puts "Debugging enabled"end

You then have access to @ivars and subject / let(:var) contents at this point.


I like @jon-rowe's solution (no additional gems needed) with a slight edit: I really don't care about other errors as much as RSpec::Expectations::ExpectationNotMetError.

config.around(:each) do |example|  example.run.tap do |result|    debugger if result.is_a?(RSpec::Expectations::ExpectationNotMetError)  endend