我已经为我的Cucumber功能添加了一个Around钩子,我曾希望在抛出异常时让pry-rescue启动pry:
Around do |scenario, block|
Pry::rescue do
block.call
end
end
Run Code Online (Sandbox Code Playgroud)
肯定会调用Around钩子,但步骤中抛出的异常不会被拯救.例如这一步:
When(/^I perform the action$/) do
raise 'hell'
end
Run Code Online (Sandbox Code Playgroud)
...导致该功能失败,但不会让我在控制台撬到它.
是否有可能使用黄瓜撬救?我也提出这个问题,因为我怀疑它可能是一个错误.
更新:根据AdamT在评论中的建议,我:
@allow-rescue标签添加到调用故意破坏步骤的功能部件中puts日志记录以验证Around是否正在调用挂钩当异常被提出时,它仍然无法进入pry,但我可以从puts语句中看到它正在进入Around钩子.
我想做同样的事情 - 在步骤失败时进行调试.您的钩子无法工作,因为已经捕获了失败的步骤异常.似乎没有标准的方法来做你想要的黄瓜.但是如果你看一下lib/cucumber/ast/step_invocation.rb这个invoke(runtime, configuration)方法,你会看到我在说什么.
在方法步骤中捕获异常.最后一个rescue块是我们要插入调试代码的地方.所以在最新的cucumber1.3.12中,在第74行插入:
require 'byebug'
byebug
Run Code Online (Sandbox Code Playgroud)
现在一旦发生瞬态故障,我得到一个提示:
[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
Run Code Online (Sandbox Code Playgroud)
您可以在其中插入其他调试代码.
我在想黄瓜项目是否会接受一个贡献来代替那里.
更新:这是我的最新版本.该版本的好处是你在进入调试器之前得到了失败日志.此外,你可以达到(至少撬)黄瓜,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
Run Code Online (Sandbox Code Playgroud)