簡體   English   中英

使用pry-rescue來調試Cucumber步驟中的異常

[英]Using pry-rescue to debug exceptions in Cucumber steps

我已經為我的Cucumber功能添加了一個Around鈎子,我曾希望在拋出異常時讓pry-rescue啟動pry:

Around do |scenario, block|
  Pry::rescue do
    block.call
  end
end

肯定會調用Around鈎子,但步驟中拋出的異常不會被挽救。 例如這一步:

When(/^I perform the action$/) do
  raise 'hell'
end

...導致該功能失敗,但不會讓我在控制台撬到它。

是否有可能使用黃瓜撬救? 我也提出這個問題 ,因為我懷疑它可能是一個錯誤。

更新:根據AdamT在評論中的建議,我:

  • @allow-rescue標簽添加到調用故意破壞步驟的功能中
  • 添加了puts記錄以驗證是否正在調用Around鈎子

當引發異常時,它仍然無法進入pry,但我可以從puts語句中看到它正在進入Around鈎子。

我想做同樣的事情 - 在步驟失敗時進行調試。 您的鈎子無法工作,因為已經捕獲了失敗的步驟異常。 似乎沒有標准的方法來做你想要的黃瓜。 但是如果你看一下lib/cucumber/ast/step_invocation.rb invoke(runtime, configuration)方法,你會看到我在說什么。

在方法步驟中捕獲異常。 最后一個rescue塊是我們要插入調試代碼的地方。 所以在最新的cucumber 1.3.12中,在第74行我插入:

        require 'byebug'
        byebug

現在一旦發生瞬態故障,我得到一個提示:

[71, 80] in /home/remote/akostadi/.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.10/lib/cucumber
/ast/step_invocation.rb
   71:             failed(configuration, e, false)
   72:             status!(:failed)
   73:           rescue Exception => e
   74:             require 'byebug'
   75:             byebug
=> 76:             failed(configuration, e, false)
   77:             status!(:failed)
   78:           end
   79:         end
   80:       end

您可以在其中插入其他調試代碼。

我在想黃瓜項目是否會接受一個貢獻來代替那里。

更新:這是我的最新版本。 該版本的好處是你在進入調試器之前得到了失敗日志。 此外,你可以達到(至少撬)黃瓜World並啟動撬內部玩,好像這是你的測試代碼。 在cuke google小組中開了一個討論,看看是否可以在上游實現類似的東西。 如果你想讓它成為黃瓜標准,請給出你的聲音和建議。 所以只需將下面的代碼放在support/env.rb

  Cucumber::Ast::StepInvocation.class_eval do
    ## first make sure we don't lose original accept method
    unless self.instance_methods.include?(:orig_accept)
      alias_method :orig_accept, :accept
    end

    ## wrap original accept method to catch errors in executed step
    def accept(visitor)
      orig_accept(visitor)
      if @exception
        unless @exception.class.name.start_with?("Cucumber::")
          # @exception = nil # to continue with following steps
          # cd visitor.runtime/@support_code
          # cd @programming_languages[0].current_world
          # binding.pry
          require 'pry'
          binding.pry
        end
      end
    end
  end

在Cucumber 2.4.0的版本中, #accept方法駐留在Cucumber::Formatter::LegacyApi::Ast::StepInvocation因此重新定義它並在其中繼續執行所需的操作:

Cucumber::Formatter::LegacyApi::Ast::StepInvocation.class_eval do
  alias_method :orig_accept, :accept

  def accept formatter
    orig_accept(formatter)
    if status == :failed
      formatter.runtime.support_code.ruby.current_world.instance_eval do
        # do something as if you are inside the cuke test step
        # like: expect(something).to be_something_else
      end
    end
  end
end

你試過打電話:

binding.pry

只需在失敗的測試中調用它並瀏覽一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM