我如何制作复杂的过滤器作为例子

时间:2013-06-17 12:09:46

标签: ruby-on-rails ruby testing bdd rspec2

我尝试使用复杂的过滤器作为示例。 我有这段代码:

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

enybody知道如何让它正常工作吗?感谢。

1 个答案:

答案 0 :(得分: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的文档。