2014-09-28 40 views
4

我要訪問命令行通過如何在RSpec的before(:suite)/ before(:all)鉤子中訪問標籤過濾器?

命令行

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)鉤我要訪問的標籤過濾器在命令行中指定的過濾器。

根據rspec核心代碼庫,包含標記過濾器存儲在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 

但是,出於某種原因,我得到一個空的哈希值,甚至當我在命令行中傳遞的標籤。

+0

這看起來有趣的是,你能否也請分享任何額外的信息;一些錯誤消息或期望與您獲得信息。 – vee 2014-09-28 02:00:14

+0

我確定你已經閱讀['--tag option'](https://www.relishapp.com/rspec/rspec-core/v/2-4/docs/command-line/tag-option )。 – vee 2014-09-28 02:01:30

+0

@vee,實質上我需要訪問命令行中指定的標記過濾器。我希望可以從上下文中提取過濾器,或者從之前的鉤子獲取塊參數。 – 2014-09-28 05:00:50

回答

2

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)

+0

看起來很有趣。你有沒有鏈接到這個文件? – 2016-01-31 20:36:00

+0

我沒有看到任何文檔。我添加了鏈接到源代碼。 – 2016-01-31 20:53:21

+0

謝謝+1。我會測試一下。 – 2016-01-31 22:53:51

相關問題