在示例中使用 skip

RSpec 提供多种方法来指示应跳过示例而不执行。

未提供实现

给定一个名为“examplewithoutblock_spec.rb”的文件,其中包含

RSpec.describe "an example" do
  it "is a skipped example"
end

我运行 rspec example_without_block_spec.rb

退出状态应为 0

并且输出应包含“1 个示例,0 个失败,1 个挂起”

并且输出应包含“尚未实现”

并且输出应包含“examplewithoutblock_spec.rb:2”。

使用 skip 跳过

给定一个名为“skipped_spec.rb”的文件,其中包含

RSpec.describe "an example" do
  skip "is skipped" do
  end
end

我运行 rspec skipped_spec.rb

退出状态应为 0

并且输出应包含“1 个示例,0 个失败,1 个挂起”

并且输出应包含

Pending: (Failures listed here are expected and do not affect your suite's status)

  1) an example is skipped
     # No reason given
     # ./skipped_spec.rb:2

在示例内部使用 skip 跳过

给定一个名为“skipped_spec.rb”的文件,其中包含

RSpec.describe "an example" do
  it "is skipped" do
    skip
  end
end

我运行 rspec skipped_spec.rb

退出状态应为 0

并且输出应包含“1 个示例,0 个失败,1 个挂起”

并且输出应包含

Pending: (Failures listed here are expected and do not affect your suite's status)

  1) an example is skipped
     # No reason given
     # ./skipped_spec.rb:2

通过在 itspecifyexample 前面加一个 x 来暂时跳过

给定一个名为“temporarilyskippedspec.rb”的文件,其中包含

RSpec.describe "an example" do
  xit "is skipped using xit" do
  end

  xspecify "is skipped using xspecify" do
  end

  xexample "is skipped using xexample" do
  end
end

我运行 rspec temporarily_skipped_spec.rb

退出状态应为 0

并且输出应包含“3 个示例,0 个失败,3 个挂起”

并且输出应包含

Pending: (Failures listed here are expected and do not affect your suite's status)

  1) an example is skipped using xit
     # Temporarily skipped with xit
     # ./temporarily_skipped_spec.rb:2

  2) an example is skipped using xspecify
     # Temporarily skipped with xspecify
     # ./temporarily_skipped_spec.rb:5

  3) an example is skipped using xexample
     # Temporarily skipped with xexample
     # ./temporarily_skipped_spec.rb:8

使用元数据跳过

给定一个名为“skipped_spec.rb”的文件,其中包含

RSpec.describe "an example" do
  example "is skipped", :skip => true do
  end
end

我运行 rspec skipped_spec.rb

退出状态应为 0

并且输出应包含“1 个示例,0 个失败,1 个挂起”

并且输出应包含

Pending: (Failures listed here are expected and do not affect your suite's status)

  1) an example is skipped
     # No reason given
     # ./skipped_spec.rb:2

使用带原因的元数据跳过

给定一个名为“skippedwithreason_spec.rb”的文件,其中包含

RSpec.describe "an example" do
  example "is skipped", :skip => "waiting for planets to align" do
    raise "this line is never executed"
  end
end

我运行 rspec skipped_with_reason_spec.rb

退出状态应为 0

并且输出应包含“1 个示例,0 个失败,1 个挂起”

并且输出应包含

Pending: (Failures listed here are expected and do not affect your suite's status)

  1) an example is skipped
     # waiting for planets to align
     # ./skipped_with_reason_spec.rb:2