2

我尝试为示例做复杂的过滤器。我有这个代码:

require 'rspec'

RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.filter_run :foo => true
end

describe 'Filtering' do

  tested_text = 'foooobar'

  [:foo, :bar].each do |location|
    [:first, :second].each do |current|
      describe 'aaa ' + location.to_s, location => true do

        before :all, location => true do
          puts location
        end

        describe 'bbbb '+ current.to_s, current => true do

          before :all, current => true do
            puts current
          end

          it 'case 1 ' do
            puts 'case 1 ' + tested_text.to_s
          end
        end
      end
    end
  end

  after :each do
    puts 'refresh doc'
  end
end

当我运行“rspec,我有一些输出

foo
first
case 1 foooobar
refresh doc
foo
second
case 1 foooobar
refresh doc

2 examples, 0 failures, 2 passed

Finished in 0.006087512 seconds

但是如果我只想运行一个示例并将这一行添加到 Rspec.configure

config.filter_run :first => true

我想得到

foo
first
case 1 foooobar
refresh doc

但是当我有一些意想不到的输出之后

foo
first
case 1 foooobar
refresh doc
foo
second
case 1 foooobar
refresh doc
bar
first
case 1 foooobar
refresh doc

3 examples, 0 failures, 3 passed

Finished in 0.011501239 seconds

有人知道如何使它正常工作吗?谢谢。

4

1 回答 1

1

当您指定两个filter_run调用时,rspec 似乎将它们视为condition_a OR condition_b

您可以将两个条件合二为一:

RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.filter_run :foo => lambda {|v, m| m[:foo] == true and m[:first] == true}
end

# or (may be easier if you have many conditions to check)
RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.filter_run :foo => lambda {|v, m| [:foo, :first].all?{|k| m[k]} }
end

看看文档filter_run

于 2013-06-17T13:24:58.717 回答