【问题标题】:How to examine rspec variables with pry debugger如何使用 pry 调试器检查 rspec 变量
【发布时间】:2013-07-24 03:46:46
【问题描述】:

我看到一些 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


    【解决方案1】:

    您应该将binding.pry 放在it 块内。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 如果您的项目将pry 定义为开发依赖项,而不是运行时依赖项,则可能不建议将require 'pry' 添加到spec_helper.rb 文件中。 Pry 是一个开发 gem,可能不推荐在您的库版本中使用。
      【解决方案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

      【讨论】:

      • 谢谢。您的回答也有效,但正如 Sergey 首先回答的那样,我不得不接受他的回答。
      【解决方案4】:

      我尝试在实际使用时要求 pry,否则,它可能会引入一些不寻常的错误(不寻常但可能会发生) 这意味着如果您只想调试特定的测试,则不需要在您的spec_helper.rb 中使用require 'pry',您可以在要调试的测试中使用require 'pry'

      def method_i_am_debugging
        puts 'does stuff'
        require 'pry'
        binding.pry
        # ...
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-19
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-03
        相关资源
        最近更新 更多