RSpecは現在のスコープをRSpec.current_scopeとして提供します
ヘルパーメソッドやライブラリコードが実行されているRSpecのスコープを検出することができます。 これは、例えば、ある特定のコンテキストでのみ呼び出すことが意味を持つメソッドの場合に便利です。
現在のスコープの検出
「current_scope_spec.rb」という名前のファイルがあるとします。
# Outside of the test lifecycle, the current scope is `:suite`
exit(1) unless RSpec.current_scope == :suite
at_exit do
  exit(1) unless RSpec.current_scope == :suite
end
RSpec.configure do |c|
  c.before :suite do
    expect(RSpec.current_scope).to eq(:before_suite_hook)
  end
  c.before :context do
    expect(RSpec.current_scope).to eq(:before_context_hook)
  end
  c.before :example do
    expect(RSpec.current_scope).to eq(:before_example_hook)
  end
  c.around :example do |ex|
    expect(RSpec.current_scope).to eq(:before_example_hook)
    ex.run
    expect(RSpec.current_scope).to eq(:after_example_hook)
  end
  c.after :example do
    expect(RSpec.current_scope).to eq(:after_example_hook)
  end
  c.after :context do
    expect(RSpec.current_scope).to eq(:after_context_hook)
  end
  c.after :suite do
    expect(RSpec.current_scope).to eq(:after_suite_hook)
  end
end
RSpec.describe "RSpec.current_scope" do
  before :context do
    expect(RSpec.current_scope).to eq(:before_context_hook)
  end
  before :example do
    expect(RSpec.current_scope).to eq(:before_example_hook)
  end
  around :example do |ex|
    expect(RSpec.current_scope).to eq(:before_example_hook)
    ex.run
    expect(RSpec.current_scope).to eq(:after_example_hook)
  end
  after :example do
    expect(RSpec.current_scope).to eq(:after_example_hook)
  end
  after :context do
    expect(RSpec.current_scope).to eq(:after_context_hook)
  end
  it "is :example in an example" do
    expect(RSpec.current_scope).to eq(:example)
  end
  it "works for multiple examples" do
    expect(RSpec.current_scope).to eq(:example)
  end
  describe "in nested describe blocks" do
    it "still works" do
      expect(RSpec.current_scope).to eq(:example)
    end
  end
end
「rspec current_scope_spec.rb」と実行すると、
すべての例がパスするはずです。