在RSpec中使用循环时输出一般行号

时间:2016-09-14 15:21:08

标签: rspec metaprogramming rspec-rails

问题

是否可以使用示例和rspec ./spec/whatever_spec.rb[1:3:1]格式的嵌套ID来显示RSpec失败,以显示感觉不明确的行号?可以在RSpec本身中完成,还是在__LINE__中使用class_eval '', __FILE__, __LINE__时使用与rspec ./spec/whatever_spec.rb[1:3:1]类似的内容?

背景

或如何阅读RSpec.describe do context 'first' do it "is not > 1" do expect(0).to_not be > 1 end end context 'second' do it "is not > 1" do expect(1).to_not be > 1 end end context 'third' do it "is not > 1" do # This is the test on line 15 expect(2).to_not be > 1 # Our failing spec end end end

如果你曾经见过上述输出,你可能会发现很难找出哪个测试失败了。

根据this answer的原因是

  

"当行号不足时,RSpec使用示例id   唯一标识有问题的例子。"

如果我们使用一个人为的例子来说明结构,那么spec文件看起来就像

rspec ./spec/whatever_spec.rb:15

这只是用于解释结构的说明。这个输出实际上是RSpec.describe do %w(first second third).each_with_index do |nth, i| context "#{nth}" do it "is not > 1" do expect(i).to_not be > 1 end end end end 。我们实际的元编程测试使得查看结构变得有点困难,因为它使用了循环。此错误实际上来自以下

[1:3:1]

{{1}}可以从右到左阅读" 第一个测试第一个上下文块正在描述的强>等级。"

0 个答案:

没有答案