--example-matches
选项
使用--example-matches
(或-E
)选项通过 REGEX 过滤示例名称。
该参数与示例的完整描述进行匹配,该描述是组描述(包括任何嵌套组)和示例描述的串联。
这使您可以运行单个具有唯一名称的示例、所有具有类似名称的示例、所有在具有唯一名称的组中的示例,等等。
您也可以多次使用该选项来指定多个示例匹配项。
注意:没有描述的示例(通常在使用 单行语法 时)无法使用此选项直接过滤,因为必须执行示例才能生成描述,因此 RSpec 无法使用尚未生成的描述来确定是否执行示例。当然,您可以传递组描述的一部分来选择组中定义的所有示例(包括那些没有描述的示例)。
背景
给定一个名为“first_spec.rb”的文件,其中包含
RSpec.describe "first group" do
it "first" do; end
it "first example in first group" do; end
it "second example in first group" do; end
end
以及一个名为“second_spec.rb”的文件,其中包含
RSpec.describe "second group" do
it "first example in second group" do; end
it "second example in second group" do; end
end
以及一个名为“third_spec.rb”的文件,其中包含
RSpec.describe "third group" do
it "first example in third group" do; end
context "group of nest" do
it "first example in nested group" do; end
it "second example in nested group" do; end
it "third example in nested_group with underscore" do; end
end
end
以及一个名为“fourth_spec.rb”的文件,其中包含
RSpec.describe Array do
describe "#length" do
it "is the number of items" do
expect(Array.new([1,2,3]).length).to eq 3
end
end
end
无匹配项
当我运行rspec . --example-matches nothing_like_this
那么该进程应该成功,即使没有运行任何示例。
匹配一个词
当我运行rspec . --example-matches example
那么所有示例都应该通过。
每个上下文中匹配一次
当我运行rspec . --example-matches 'first example'
那么所有示例都应该通过。
在一个文件中匹配一次,仅使用示例名称
当我运行rspec . --example-matches 'first example in first group'
那么所有示例都应该通过。
在一个文件中匹配一次,使用示例名称和组名称
当我运行rspec . --example-matches 'first group first example in first group'
那么所有示例都应该通过。
一个组中的所有示例
当我运行rspec . --example-matches 'first group'
那么所有示例都应该通过。
在一个文件中匹配一次,使用组名称
当我运行rspec . --example-matches 'second group first example'
那么所有示例都应该通过。
一个组中的所有示例,包括嵌套组中的示例
当我运行rspec . --example-matches 'third group'
那么所有示例都应该通过。
使用ClassName#method_name
形式匹配
当我运行rspec . --example-matches 'Array#length'
那么所有示例都应该通过。
仅匹配匹配的正则表达式
当我运行rspec . --example-matches "first$" --format d
那么所有示例都应该通过
以及输出应包含所有这些内容
first |
以及输出不应包含任何这些内容
first example in first group |
second example in first group |
first example in second group |
second example in second group |
first example in third group |
nested group first example in nested group |
nested group second example in nested group |
仅匹配匹配的正则表达式,带词边界
当我运行rspec . --example-matches "nested[^_]" --format d
那么所有示例都应该通过
以及输出应包含所有这些内容
first example in nested group |
second example in nested group |
以及输出不应包含任何这些内容
first example in first group |
second example in first group |
first example in second group |
second example in second group |
first example in third group |
third example in nested_group |
多次应用示例名称选项
当我运行rspec . --example-matches 'first group' --example-matches 'second group' --format d
那么所有示例都应该通过
以及输出应包含所有这些内容
first example in first group |
second example in first group |
first example in second group |
second example in second group |
以及输出不应包含任何这些内容
first example in third group |
nested group first example in nested group |
nested group second example in nested group |