从元数据访问“预期”行
Access `expected` line from metadata
我想要输出行,在示例中 rspec 比较期间失败了,但我不知道如何做到最好。
例如我有这样的测试:
require 'rspec'
describe 'My behaviour' do
it 'should do something' do
test_string = 'test'
expect(test_string).to eq('failed_test')
end
after :each do |example|
puts example.metadata[:expect_line]
end
end
我想 after :each
中的输出行是
"expect(test_string).to eq('failed_test')"
我知道,我可以访问 example.metadata[:location]
,其中 return 类似于 "./spec/test_spec.rb:4"
,我可以解析它并提取行,但是是否已经有像我需要隐藏的东西example
结构?
更新:
我才明白。 example.metadata[:location]
return 不是失败的行,而是 it
开始的行,所以它对我没有用:(
所以问题仍然存在 - 如何获得失败的线路?
据我所知,此信息并未隐藏在示例结构中的任何位置。 RSpec 的默认输出显示失败的行:
Failures:
1) My Description should fail
Failure/Error: expect(1).to eq(2)
expected: 2
got: 1
如果我们看看 rspec 本身是如何在 rspec/core/formatters/exception_presenter.rb and rspec/core/formatters/snippet_extractor.rb 中得到这个的,看起来他们通过示例异常的回溯来找到规范文件并提取行(类似于什么你提到的)。如果有更简单的方法将其从示例中提取出来,我认为 RSpec 本身会使用它 :)
我想要输出行,在示例中 rspec 比较期间失败了,但我不知道如何做到最好。 例如我有这样的测试:
require 'rspec'
describe 'My behaviour' do
it 'should do something' do
test_string = 'test'
expect(test_string).to eq('failed_test')
end
after :each do |example|
puts example.metadata[:expect_line]
end
end
我想 after :each
中的输出行是
"expect(test_string).to eq('failed_test')"
我知道,我可以访问 example.metadata[:location]
,其中 return 类似于 "./spec/test_spec.rb:4"
,我可以解析它并提取行,但是是否已经有像我需要隐藏的东西example
结构?
更新:
我才明白。 example.metadata[:location]
return 不是失败的行,而是 it
开始的行,所以它对我没有用:(
所以问题仍然存在 - 如何获得失败的线路?
据我所知,此信息并未隐藏在示例结构中的任何位置。 RSpec 的默认输出显示失败的行:
Failures:
1) My Description should fail
Failure/Error: expect(1).to eq(2)
expected: 2
got: 1
如果我们看看 rspec 本身是如何在 rspec/core/formatters/exception_presenter.rb and rspec/core/formatters/snippet_extractor.rb 中得到这个的,看起来他们通过示例异常的回溯来找到规范文件并提取行(类似于什么你提到的)。如果有更简单的方法将其从示例中提取出来,我认为 RSpec 本身会使用它 :)