around フック
around フックは、例をブロック引数として受け取り、プロックとして振る舞うように拡張されます。これにより、例の前後に実行されるコードを定義することができます。もちろん、同じことは before フックと after フックでも行うことができますが、しばしばより簡潔になります。
around フックが輝くのは、例をブロック内で実行したい場合です。たとえば、データベースライブラリがブロックを受け取るトランザクションメソッドを提供している場合、around を使用して例の周りでトランザクションをきれいに開始し、終了することができます。
注意: around フックは、before フックや after フックとは異なり、例と状態を共有しません。つまり、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 を実行すると
出力には "this should show up in the output" という内容が含まれるはずです。