在 Rspec 中运行嵌套标签 [英] Running Nested Tags in Rspec

查看:40
本文介绍了在 Rspec 中运行嵌套标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以这种方式构建我的测试,以便我可以自己运行某些上下文块,但还需要进一步在各个 it 块中强制执行嵌套标签.像这样:

I'm trying to architect my tests in such a way so I can run certain context blocks by themselves, but also need to further enforce nested tags in individual it blocks. Something like this:

context 'outer context', :outer_tag do
    it 'inner it', :tag1 do
        expect(1).to eq(1)
    end

    it 'inner it 2', :tag2 do
        expect(2).to eq(2)
    end
end

我想按照以下方式运行:

and I want to run something along the lines of:

rspec --tag 外标签 --tag tag1

rspec --tag outer-tag --tag tag1

希望它只在用 :outer-tag 标记的上下文中运行测试,而这些上下文本身用 :tag1 标记

in the hopes that it will only run tests within the context tagged with :outer-tag that are themselves tagged with :tag1

有没有办法获得这种行为?目前,当我想我正在寻找它作为更多的和"操作时,这似乎作为或"操作.

Is there a way to get this behavior? Currently this seems to operate as an 'or' when I guess I am looking for it to operate as more of an 'and'.

谢谢!

推荐答案

你可以这样做:

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

当您需要多个级别时,这开始变得奇怪:

This starts to get weird when you need multiple levels though:

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 在命令行(散列)上处理标签的方式,它可能会导致一些意想不到的东西试图组合它们:

And because of the way rspec handles tags on the command line (hashes), it can lead to some unexpected stuff trying to combine them:

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

这篇关于在 Rspec 中运行嵌套标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆