Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Around hooks don't receive example #1619

@sj26

Description

@sj26

Accessing example within hooks is now deprecated in favour of using example as yielded into the block, but around hooks don't received the example as a parameter but instead just received a proc:

def run
hooks.inject(@initial_procsy) do |procsy, around_hook|
procsy.wrap do
@example.instance_exec(procsy, &around_hook.block)
end
end.call
end

This means we must fall back to using RSpec.current_example which reeks of accessing Thread.current and is inconsistent.

Example:

gem "rspec", "3.0.0"
require "rspec/autorun"
require "pp"

RSpec.configure do |config|
  config.around do |*args|
    p around: args, current_example: RSpec.current_example
    args.first.call
  end
end

describe "example" do
  it "passes" do
    expect(true).to be_truthy
  end

  it "fails" do
    expect(false).to be_truthy
  end
end

Yields:

{:around=>
  [#<Proc:0x007f93fc65b458@/Users/sj26/.rbenv/gems/2.1.0/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:145>],
 :current_example=>
  #<RSpec::Core::Example:0x007f93fc682cd8
   @around_example_hooks=
    [#<RSpec::Core::Hooks::Hook:0x007f93fca72d48
      @block=#<Proc:[email protected]:6>,
      @options={}>],
   @clock=RSpec::Core::Time,
   @example_block=#<Proc:[email protected]:13>,
   @example_group_class=RSpec::ExampleGroups::Example,
   @example_group_instance=
    #<RSpec::ExampleGroups::Example:0x007f93fc65b5e8 @__memoized=nil>,
   @exception=nil,
   @metadata=
    {:execution_result=>
      #<RSpec::Core::Example::ExecutionResult:0x007f93fc682b20
       @started_at=2014-06-26 10:30:12 +0800>,
     :block=>#<Proc:[email protected]:13>,
     :description_args=>["passes"],
     :description=>"passes",
     :full_description=>"example passes",
     :described_class=>nil,
     :file_path=>"example.rb",
     :line_number=>13,
     :location=>"example.rb:13",
     :example_group=>
      {:execution_result=>
        #<RSpec::Core::Example::ExecutionResult:0x007f93fc683b60>,
       :block=>#<Proc:[email protected]:12>,
       :description_args=>["example"],
       :description=>"example",
       :full_description=>"example",
       :described_class=>nil,
       :file_path=>"example.rb",
       :line_number=>12,
       :location=>"example.rb:12"}}>}
{:around=>
  [#<Proc:0x007f93fc61aed0@/Users/sj26/.rbenv/gems/2.1.0/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:145>],
 :current_example=>
  #<RSpec::Core::Example:0x007f93fc682788
   @around_example_hooks=
    [#<RSpec::Core::Hooks::Hook:0x007f93fca72d48
      @block=#<Proc:[email protected]:6>,
      @options={}>],
   @clock=RSpec::Core::Time,
   @example_block=#<Proc:[email protected]:17>,
   @example_group_class=RSpec::ExampleGroups::Example,
   @example_group_instance=
    #<RSpec::ExampleGroups::Example:0x007f93fc61b010 @__memoized=nil>,
   @exception=nil,
   @metadata=
    {:execution_result=>
      #<RSpec::Core::Example::ExecutionResult:0x007f93fc6825d0
       @started_at=2014-06-26 10:30:12 +0800>,
     :block=>#<Proc:[email protected]:17>,
     :description_args=>["fails"],
     :description=>"fails",
     :full_description=>"example fails",
     :described_class=>nil,
     :file_path=>"example.rb",
     :line_number=>17,
     :location=>"example.rb:17",
     :example_group=>
      {:execution_result=>
        #<RSpec::Core::Example::ExecutionResult:0x007f93fc683b60>,
       :block=>#<Proc:[email protected]:12>,
       :description_args=>["example"],
       :description=>"example",
       :full_description=>"example",
       :described_class=>nil,
       :file_path=>"example.rb",
       :line_number=>12,
       :location=>"example.rb:12"}}>}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions