使用pry-rescue调试Cucumber步骤中的异常 [英] Using pry-rescue to debug exceptions in Cucumber steps

查看:107
本文介绍了使用pry-rescue调试Cucumber步骤中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的黄瓜功能中添加了一个环绕钩子,我希望该钩子会在引发异常时使撬动救援开始撬动。

I've added an Around hook to my Cucumber features that I had hoped would cause pry-rescue to start pry when an exception was thrown:

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

肯定会调用Around钩子,但是无法挽救在步骤中抛出的异常。例如。此步骤:

The Around hook is definitely being called, however exceptions thrown within steps aren't rescued. E.g. this step:

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

...导致功能失败,但不会让我大惊小怪控制台。

... causes the feature to fail, but doesn't drop me into pry at the console.

是否可以在黄瓜上使用撬式救援?我也将其作为 issue 提出,我怀疑这可能是

Is it possible to use pry-rescue with Cucumber? I've raised this as an issue as well, as I suspect it might be a bug.

已更新:根据AdamT在评论中的建议,我已经:

Updated: as per a suggestion from AdamT in the comments, I've:


  • @ allow-rescue 标记添加到调用故意中断的步骤的功能中

  • 增加了 puts 日志记录以验证 Around 挂钩是否被调用

  • added the @allow-rescue tag to the feature calling the deliberately broken step
  • added puts logging to verify that the Around hook is being called

在引发异常时仍无法输入撬动,但从 puts 语句中可以看到它正在进入

It's still failing to enter pry when the exception is raised, but I can see from the puts statements that it's entering the Around hook.

推荐答案

我想做同样的事情-在步骤失败时进行调试。您的挂钩无法工作,因为已经捕获了失败的步骤异常。似乎没有标准方法可以用来做黄瓜。但是,如果您查看 lib / cucumber / ast / step_invocation.rb invoke(运行时,配置)方法,会看到我在说什么。

I wanted to do the same thing - debug when a step fails. Your hook cannot work because a failing step exception is caught already. There seems to be no standard way of doing what you want with cucumber. But if you look at lib/cucumber/ast/step_invocation.rb the invoke(runtime, configuration) method, you will see what I am talking about.

在方法步骤级别中捕获异常。最后一个 rescue 块是我们要插入调试代码的位置。因此,在最新的黄瓜 1.3.12中,我在第74行插入:

In the method step level exceptions are caught. And the last rescue block is where we want to insert our debugging code. So in latest cucumber 1.3.12, on line 74 I inserted:

        require 'byebug'
        byebug

现在一旦发生瞬时故障,我会得到提示:

And now once the transient failure happens I get a prompt:

[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

虽然您可以在其中插入其他调试代码。

You can insert other debugging code in there though.

我是想想黄瓜项目是否会接受捐款,而不是在那里钩上钩子。

I'm thinking if cucumber project will accept a contribution to have a hook there instead.

更新:这是我的最新版本。该版本的优点是可以在进入调试器之前获取失败日志。您也可以(至少通过撬动)到达黄瓜 World 并在其中启动撬动,就像在测试代码一样。我已经在库克Google组中打开了讨论以查看如果可以在上游实施类似的措施。如果您想将其作为黄瓜的标准配菜,请给出您的声音和建议。
因此,只需将以下代码放入 support / env.rb

UPDATE: here's my latest version. The positives in that version are that you get failure log before falling into a debugger. Also you can reach (at least with pry) to the cucumber World and launch pry inside to play around as if this is your test code. I've opened a discussion in the cuke google group to see if something similar can be implemented upstream. Give your voice and suggestions if you want to have it standard in cucumber. So just put the below code in 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

这篇关于使用pry-rescue调试Cucumber步骤中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆