around 钩子

around 钩子接收示例作为块参数,扩展为像 proc 一样。这使您可以定义应在示例之前和之后执行的代码。当然,您也可以使用beforeafter 钩子来做同样的事情;并且这样做通常更干净。

around 钩子的优势在于,当您想在块中运行示例时。例如,如果您的数据库库提供了接收块的交易方法,您可以使用around 在示例周围干净地打开和关闭交易。

警告:around 钩子不与示例共享状态,就像beforeafter 钩子一样。这意味着您不能在around 钩子和示例之间共享实例变量。

警告:模拟框架在运行示例的上下文中设置和拆除。您不能直接在around 钩子中与它们交互。

警告:around 钩子将在任何before 钩子之前执行,并在任何after 钩子之后执行,无论它们是在哪个上下文中定义的。

在传递给around() 的块中使用示例作为 proc

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

class Database
  def self.transaction
    puts "open transaction"
    yield
    puts "close transaction"
  end
end

RSpec.describe "around filter" do
  around(:example) do |example|
    Database.transaction(&example)
  end

  it "gets run in order" do
    puts "run the example"
  end
end

我运行rspec example_spec.rb

那么输出应该包含

open transaction
run the example
close transaction

使用run() 调用示例

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

RSpec.describe "around hook" do
  around(:example) do |example|
    puts "around example before"
    example.run
    puts "around example after"
  end

  it "gets run in order" do
    puts "in the example"
  end
end

我运行rspec example_spec.rb

那么输出应该包含

around example before
in the example
around example after

访问示例元数据

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

RSpec.describe "something" do
  around(:example) do |example|
    puts example.metadata[:foo]
    example.run
  end

  it "does something", :foo => "this should show up in the output" do
  end
end

我运行rspec example_spec.rb

那么输出应该包含“这应该出现在输出中”。

即使示例抛出异常,around 钩子也会继续运行

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

  RSpec.describe "something" do
    around(:example) do |example|
      puts "around example setup"
      example.run
      puts "around example cleanup"
    end

    it "still executes the entire around hook" do
      fail "the example blows up"
    end
  end

我运行rspec example_spec.rb

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

并且输出应该包含

around example setup
around example cleanup

定义全局around 钩子

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

RSpec.configure do |c|
  c.around(:example) do |example|
    puts "around example before"
    example.run
    puts "around example after"
  end
end

RSpec.describe "around filter" do
  it "gets run in order" do
    puts "in the example"
  end
end

我运行rspec example_spec.rb

那么输出应该包含

around example before
in the example
around example after

每个示例钩子都由around 钩子包装

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

RSpec.describe "around filter" do
  around(:example) do |example|
    puts "around example before"
    example.run
    puts "around example after"
  end

  before(:example) do
    puts "before example"
  end

  after(:example) do
    puts "after example"
  end

  it "gets run in order" do
    puts "in the example"
  end
end

我运行rspec example_spec.rb

那么输出应该包含

around example before
before example
in the example
after example
around example after

上下文钩子没有被around 钩子包装

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

RSpec.describe "around filter" do
  around(:example) do |example|
    puts "around example before"
    example.run
    puts "around example after"
  end

  before(:context) do
    puts "before context"
  end

  after(:context) do
    puts "after context"
  end

  it "gets run in order" do
    puts "in the example"
  end
end

我运行rspec --format progress example_spec.rb

那么输出应该包含

before context
around example before
in the example
around example after
.after context

around 块运行的示例在配置的上下文中运行

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

module IncludedInConfigureBlock
  def included_in_configure_block; true; end
end

RSpec.configure do |c|
  c.include IncludedInConfigureBlock
end

RSpec.describe "around filter" do
  around(:example) do |example|
    example.run
  end

  it "runs the example in the correct context" do
    expect(included_in_configure_block).to be(true)
  end
end

我运行rspec example_spec.rb

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

隐式待定示例被检测为未实现

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

RSpec.describe "implicit pending example" do
  around(:example) do |example|
    example.run
  end

  it "should be detected as Not yet implemented"
end

我运行rspec example_spec.rb

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

并且输出应该包含

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

  1) implicit pending example should be detected as Not yet implemented
     # Not yet implemented
     # ./example_spec.rb:6

显式待定示例被检测为待定

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

RSpec.describe "explicit pending example" do
  around(:example) do |example|
    example.run
  end

  it "should be detected as pending" do
    pending
    fail
  end
end

我运行rspec example_spec.rb

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

并且输出应该包含

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

  1) explicit pending example should be detected as pending
     # No reason given

在同一范围内有多个around 钩子

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

RSpec.describe "if there are multiple around hooks in the same scope" do
  around(:example) do |example|
    puts "first around hook before"
    example.run
    puts "first around hook after"
  end

  around(:example) do |example|
    puts "second around hook before"
    example.run
    puts "second around hook after"
  end

  it "they should all be run" do
    puts "in the example"
    expect(1).to eq(1)
  end
end

我运行rspec example_spec.rb

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

并且输出应该包含

first around hook before
second around hook before
in the example
second around hook after
first around hook after

在多个范围内使用around 钩子

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

RSpec.describe "if there are around hooks in an outer scope" do
  around(:example) do |example|
    puts "first outermost around hook before"
    example.run
    puts "first outermost around hook after"
  end

  around(:example) do |example|
    puts "second outermost around hook before"
    example.run
    puts "second outermost around hook after"
  end

  describe "outer scope" do
    around(:example) do |example|
      puts "first outer around hook before"
      example.run
      puts "first outer around hook after"
    end

    around(:example) do |example|
      puts "second outer around hook before"
      example.run
      puts "second outer around hook after"
    end

    describe "inner scope" do
      around(:example) do |example|
        puts "first inner around hook before"
        example.run
        puts "first inner around hook after"
      end

      around(:example) do |example|
        puts "second inner around hook before"
        example.run
        puts "second inner around hook after"
      end

      it "they should all be run" do
        puts "in the example"
      end
    end
  end
end

我运行rspec example_spec.rb

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

并且输出应该包含

first outermost around hook before
second outermost around hook before
first outer around hook before
second outer around hook before
first inner around hook before
second inner around hook before
in the example
second inner around hook after
first inner around hook after
second outer around hook after
first outer around hook after
second outermost around hook after
first outermost around hook after