如果 rspec 测试失败,则启动 ruby 调试器
通常,当测试失败时,我会花相当长的时间试图找出导致它失败的原因.如果 RSpec 可以在测试失败时启动 Ruby 调试器,那么我可以立即检查局部变量以深入了解原因.
Often, when a test fails, I spend quite sometime trying to figure out the what caused it to fail. It'd be useful if RSpec could kick off a Ruby debugger when the test fails, so that I can inspect the local variables immediately to drill down on the cause.
我现在使用的解决方法如下所示:
The work-around I'm using right now looks something like this:
# withing some test
debugger unless some_variable.nil?
expect(some_variable).to be_nil
然而,这种方法很麻烦,因为我首先等待测试失败,然后添加调试器行,修复问题,然后必须删除调试器行,而我希望它更像 gdb
能够在遇到异常时启动,而无需使用 debugger
语句来填充您的代码库.
However, this approach is cumbersome, because I first wait for a test to fail, then add the debugger line, fix the issue and then have to remove the debugger line, whereas I want it work more like gdb
which has the ability to kick in when an exception is hit, without requiring to pepper your code base with debugger
statements.
我试过普利茅斯.它对我来说还不够可靠.此外,开发历史似乎表明它不是一个得到很好支持的 gem,所以我宁愿不依赖它.
I've tried Plymouth. It hasn't worked reliably enough for me. Also the development history seems to indicate that it isn't a very well supported gem, so I'd rather not rely on it.
更新:我尝试了 pry-rescue
并发现它很整洁.但是,我经常使用 zeus 并且想知道是否有办法让它与 pry 一起使用-救援
.
Update: I tried out pry-rescue
and find it to be neat. However, I use zeus a lot and was wondering if there's a way to make it work with pry-rescue
.
使用pry-rescue,它的精神继承者到普利茅斯:
Use pry-rescue, it's the spiritual successor to plymouth:
来自自述文件:
如果您使用的是 RSpec 或 respec,您可以使用rescue rspec 或rescue respec 在每次测试失败时打开一个pry 会话:
If you're using RSpec or respec, you can open a pry session on every test failure using rescue rspec or rescue respec:
$ rescue rspec
From: /home/conrad/0/ruby/pry-rescue/examples/example_spec.rb @ line 9 :
6:
7: describe "Float" do
8: it "should be able to add" do
=> 9: (0.1 + 0.2).should == 0.3
10: end
11: end
RSpec::Expectations::ExpectationNotMetError: expected: 0.3
got: 0.30000000000000004 (using ==)
[1] pry(main)>