包含过滤器

您可以通过声明包含过滤器来约束运行哪些示例。最常见的用例是在您专注于特定问题时集中在示例子集上。您也可以使用仅包含符号的元数据。

背景

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

RSpec.configure do |c|
  c.filter_run_including :focus => true
end

关注一个示例

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

require "spec_helper"

RSpec.describe "something" do
  it "does one thing" do
  end

  it "does another thing", :focus => true do
  end
end

我运行rspec spec/sample_spec.rb --format doc

那么输出应该包含“做另一件事”

并且输出不应包含“做一件事”。

关注一个组

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

require "spec_helper"

RSpec.describe "group 1", :focus => true do
  it "group 1 example 1" do
  end

  it "group 1 example 2" do
  end
end

RSpec.describe "group 2" do
  it "group 2 example 1" do
  end
end

我运行rspec spec/sample_spec.rb --format doc

那么输出应该包含“组 1 示例 1”

并且输出应该包含“组 1 示例 2”

并且输出不应包含“组 2 示例 1”。

在不匹配的示例组中,`before`/`after(:context)` 钩子不会运行

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

require "spec_helper"

RSpec.describe "group 1", :focus => true do
  before(:context) { puts "before all in focused group" }
  after(:context)  { puts "after all in focused group"  }

  it "group 1 example" do
  end
end

RSpec.describe "group 2" do
  before(:context) { puts "before all in unfocused group" }
  after(:context)  { puts "after all in unfocused group"  }

  context "context 1" do
    it "group 2 context 1 example 1" do
    end
  end
end

我运行rspec ./spec/before_after_all_inclusion_filter_spec.rb

那么输出应该包含“在聚焦组中之前所有”

并且输出应该包含“在聚焦组中之后所有”

并且输出不应包含“在未聚焦组中之前所有”

并且输出不应包含“在未聚焦组中之后所有”。

使用符号作为元数据

假设一个名为“symbols_as_metadata_spec.rb”的文件,其中包含

RSpec.configure do |c|
  c.filter_run :current_example
end

RSpec.describe "something" do
  it "does one thing" do
  end

  it "does another thing", :current_example do
  end
end

我运行rspec symbols_as_metadata_spec.rb --format doc

那么输出应该包含“做另一件事”

并且输出不应包含“做一件事”。