我尝试为示例做复杂的过滤器。 我有这段代码:
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
有人知道如何让它正常工作吗?谢谢。
最佳答案
当您指定两个 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
的文档.
关于ruby-on-rails - 我如何为示例制作复杂的过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17147161/