加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何在RSpec中的before(:suite)/ before(:all)钩子中

发布时间:2020-12-16 21:03:55 所属栏目:百科 来源:网络整理
导读:我想访问命令行中传递的标记过滤器 命令行 rspec --tag use_ff RSpec配置 RSpec.configure do |config| config.before :suite,type: :feature do # how do I check if use_ff filter was specified in the command line? if filter[:use_ff] use_selenium el
我想访问命令行中传递的标记过滤器

命令行

rspec --tag use_ff

RSpec配置

RSpec.configure do |config|
  config.before :suite,type: :feature do 
    # how do I check if use_ff filter was specified in the command line?
    if filter[:use_ff]
      use_selenium
    else
      use_poltergeist
    end
  end
end

在before(:suite)钩子中,我想访问config中命令行中指定的标记过滤器.

根据rspec-core代码库,包含标记过滤器存储在RSpec.configuration的inclusion_filter中.从理论上讲,我应该能够按如下方式访问它们:

RSpec.configure do |config|
  config.before :suite,type: :feature do 
    if config.filter[:use_ff] # filter is an alias for inclusion_filter
      use_selenium
    else
      use_poltergeist
    end
  end
end

但是,出于某种原因,即使从命令行传递标记,我也会得到一个空哈希.

解决方法

config.filter返回一个 RSpec::Core::InclusionRules.在它的超类 RSpec::Core::FilterRules中,我们看到它有一个访问器.rules,它返回一个标签的哈希,所以你可以做,例如,
RSpec.configure do |config|
  config.before(:suite) do
    $running_only_examples_tagged_foo = config.filter.rules[:foo]
  end
end

describe "Something" do
  it "knows we're running only examples tagged foo",:foo do
    expect($running_only_examples_tagged_foo).to be_truthy # passes
  end
end

(我正在使用RSpec 3.4.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读