排除过滤器

您可以通过声明一个排除过滤器,然后用该过滤器标记示例或整个组,来从运行中排除示例。您也可以只使用符号来指定元数据。

排除示例

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

RSpec.configure do |c|
  # declare an exclusion filter
  c.filter_run_excluding :broken => true
end

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

  # tag example for exclusion by adding metadata
  it "does another thing", :broken => true do
  end
end

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

那么输出应该包含“does one thing”

并且输出不应包含“does another thing”。

排除一个组

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

RSpec.configure do |c|
  c.filter_run_excluding :broken => true
end

RSpec.describe "group 1", :broken => 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

那么输出应该包含“group 2 example 1”

并且输出不应包含“group 1 example 1”

并且输出不应包含“group 1 example 2”。

排除多个组

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

RSpec.configure do |c|
  c.filter_run_excluding :broken => true
end

RSpec.describe "group 1", :broken => true do
  before(:context) do
    raise "you should not see me"
  end

  it "group 1 example 1" do
  end

  it "group 1 example 2" do
  end
end

RSpec.describe "group 2", :broken => true do
  before(:example) do
    raise "you should not see me"
  end

  it "group 2 example 1" do
  end
end

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

那么即使没有运行任何示例,进程也应该成功

并且输出不应包含“group 1”

并且输出不应包含“group 2”。

在排除的示例组中,before/after(:context) 钩子不运行

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

RSpec.configure do |c|
  c.filter_run_excluding :broken => true
end

RSpec.describe "group 1" do
  before(:context) { puts "before context in included group" }
  after(:context)  { puts "after context in included group"  }

  it "group 1 example" do
  end
end

RSpec.describe "group 2", :broken => true do
  before(:context) { puts "before context in excluded group" }
  after(:context)  { puts "after context in excluded group"  }

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

我运行 rspec ./spec/before_after_context_exclusion_filter_spec.rb

那么输出应该包含“before context in included group”

并且输出应该包含“after context in included group”

并且输出不应包含“before context in excluded group”

并且输出不应包含“after context in excluded group”。

使用符号作为元数据

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

RSpec.configure do |c|
  c.filter_run_excluding :broken
end

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

  # tag example for exclusion by adding metadata
  it "does another thing", :broken do
  end
end

我运行 rspec symbols_as_metadata_spec.rb --format doc

那么输出应该包含“does one thing”

并且输出不应包含“does another thing”。