Question

I've seen some SO posts explaining how to use pry to step into rspec tests and been able to do this. Once I reach the breakpoint though, I'm struggling to display any useful information. For this code below, I'd like to examine the response object from the pry console:

describe 'happenings' do
  context "#index (GET /api/v1/flat_happenings.json)" do
    before(:each) do
      30.times { FactoryGirl.create(:flat_happening) }
      get "/api/v1/flat_happenings.json"
    end
    describe "should list all flat_happenings" do
      binding.pry
      it { JSON.parse(response.body)["flat_happenings"].length.should eq 30 }
    end
  end
end

Any ideas on how to do this?

Was it helpful?

Solution

You should place binding.pry inside it block.

OTHER TIPS

To use pry in specs we need to add require 'pry' within the spec_helper.rb file. Then we are able to use binding.pry within any of the specs.

This should work:

describe 'happenings' do
  context "#index (GET /api/v1/flat_happenings.json)" do
    before(:each) do
      30.times { FactoryGirl.create(:flat_happening) }
      get "/api/v1/flat_happenings.json"
    end
    it "should list all flat_happenings" do
      binding.pry
      JSON.parse(response.body)["flat_happenings"].length.should eq 30
    end
  end
end

HTH

I try to require pry when I actually use it, otherwise, it might introduce some unusual bugs (unusual but it can happen) So that means if you only want to debug a particular test, you don't need to require 'pry' in your spec_helper.rb you can use require 'pry' just in the test you want to debug.

def method_i_am_debugging
  puts 'does stuff'
  require 'pry'
  binding.pry
  # ...
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top