現在の例を使用する
ブロック引数を使用して、例オブジェクトとそのメタデータにアクセスすることができます。it、subject、let、before、after、およびaroundフックに提供されるブロック引数を使用します。
例内からexampleオブジェクトにアクセスする
次の内容で「spec/example_spec.rb」という名前のファイルがあるとします。
RSpec.describe "example as block arg to it, before, and after" do
  before do |example|
    expect(example.description).to eq("is the example object")
  end
  after do |example|
    expect(example.description).to eq("is the example object")
  end
  it "is the example object" do |example|
    expect(example.description).to eq("is the example object")
  end
end
RSpec.describe "example as block arg to let" do
  let(:the_description) do |example|
    example.description
  end
  it "is the example object" do |example|
    expect(the_description).to eq("is the example object")
  end
end
RSpec.describe "example as block arg to subject" do
  subject do |example|
    example.description
  end
  it "is the example object" do |example|
    expect(subject).to eq("is the example object")
  end
end
RSpec.describe "example as block arg to subject with a name" do
  subject(:the_subject) do |example|
    example.description
  end
  it "is the example object" do |example|
    expect(the_subject).to eq("is the example object")
    expect(subject).to eq("is the example object")
  end
end
rspec spec/example_spec.rbを実行すると、
例はパスするはずです。