문제

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?

도움이 되었습니까?

해결책

You should place binding.pry inside it block.

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top