使用 run_all_when_everything_filtered

注意:此功能已被 filter_run_when_matching 取代,并将从 RSpec 的未来版本中删除。

使用 run_all_when_everything_filtered 选项告诉 RSpec 在您尝试运行过滤后的规范列表但没有规范匹配该过滤器的情况下运行所有规范。 当与像 :focus => true 这样的包含过滤器配对时,此方法效果很好,因为它将在没有示例匹配包含过滤器时运行所有示例。

  RSpec.configure { |c| c.run_all_when_everything_filtered = true }

背景

假设有一个名为“spec/spec_helper.rb”的文件,其中包含

RSpec.configure {|c| c.run_all_when_everything_filtered = true}

默认情况下,如果所有规范都被包含标签过滤掉,则不会运行任何规范

假设有一个名为“spec/example_spec.rb”的文件,其中包含

RSpec.describe "examples" do
  it "with no tag" do
  end

  it "with no tag as well" do
  end
end

我运行 rspec spec/example_spec.rb --tag some_tag

那么输出应包含“0 个示例,0 个失败”。

如果规范被排除标签过滤掉,它们仍然会运行

假设有一个名为“spec/example_spec.rb”的文件,其中包含

RSpec.describe "examples" do
  it "with no tag" do
  end

  it "with no tag as well" do
  end
end

我运行 rspec spec/example_spec.rb --tag ~some_tag

那么输出应包含“2 个示例,0 个失败”。

当启用 run_all_when_everything_filtered 选项时,如果过滤标签有任何匹配项,则只运行这些特性

假设有一个名为“spec/example_spec.rb”的文件,其中包含

require "spec_helper"
RSpec.describe "examples" do
  it "with no tag", :some_tag => true do
  end

  it "with no tag as well" do
  end
end

我运行 rspec spec/example_spec.rb --tag some_tag

那么输出应包含“1 个示例,0 个失败”。

并且输出应包含“运行选项:include {:some_tag=>true}”。

当启用 run_all_when_everything_filtered 选项时,当标签没有匹配项时,所有规范都会运行

假设有一个名为“spec/example_spec.rb”的文件,其中包含

require "spec_helper"
RSpec.describe "examples" do
  it "with no tag" do
  end

  it "with no tag as well" do
  end
end

我运行 rspec spec/example_spec.rb --tag some_tag

那么输出应包含“2 个示例,0 个失败”。

并且输出应包含“所有示例都被过滤掉;忽略 {:some_tag=>true}”。