你可以做這樣的事情:
RSpec.describe 'Something' do
context 'outer context', :outer_tag do
it 'inner one', :tag1, outer_tag: 'tag1' do
expect(1).to eq(1)
end
it 'inner two', :tag2, outer_tag: 'tag2' do
expect(2).to eq(2)
end
end
context 'another context', :different_tag do
it 'inner three', :tag2, different_tag: 'tag2' do
expect(3).to eq(3)
end
end
end
,然後運行:
rspec example.rb --tag 'outer_tag'
# => Something
# outer context
# inner one
# inner two
rspec example.rb --tag 'outer_tag:tag2'
# => Something
# outer context
# inner two
rspec example.rb --tag tag2
# => Something
# outer context
# inner two
# another context
# inner three
這將啓動,當你需要多層次,雖然得到怪異:
context 'third context', :final_tag do
context 'inside third', :inner_third, final_tag: 'inner_third' do
it 'inner four', :inner_four, inner_third: 'inner_four', final_tag: 'inner_third:inner_four' do
expect(4).to eq(4)
end
end
end
rspec example.rb --tag 'final_tag:inner_third:inner_four'
rspec example.rb --tag 'inner_third:inner_four'
rspec example.rb --tag inner_four
# All run
# => Something
# third context
# inside third
# inner four
作品,但是非常冗長。
而且由於RSpec的方式處理命令行(哈希)上的標籤,它會導致一些意想不到的東西,試圖把它們混合起來:
rspec example.rb --tag outer_tag --tag ~'outer_tag:tag2'
# Run options: exclude {:outer_tag=>"tag2"}
# => Something
# outer context
# inner one
# another context # <- nothing in this context was expected to run
# inner three (FAILED - 1)
這樣的工作,但:
rspec example.rb --tag outer_tag --tag ~tag2
# => Something
# outer context
# inner one