如何使用pry调试器检查rspec变量

问题描述 投票:18回答:3

我已经看到一些SO帖子解释了如何使用pry进入rspec测试并且能够做到这一点。一旦我到达断点,我就很难显示任何有用的信息。对于下面的代码,我想从pry控制台检查响应对象:

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

关于如何做到这一点的任何想法?

ruby-on-rails debugging rspec rspec-rails pry
3个回答
27
投票

你应该把binding.pry放在it区块内。


13
投票

要在specs中使用pry,我们需要在spec_helper.rb文件中添加require 'pry'。然后我们就可以在任何规范中使用binding.pry。


3
投票

这应该工作:

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

© www.soinside.com 2019 - 2024. All rights reserved.