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

Add expose_current_running_example_as config option. #1133

Merged
merged 3 commits into from
Nov 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,39 @@ def warnings
$VERBOSE
end

# Exposes the current running example via the named
# helper method. RSpec 2.x exposed this via `example`,
# but in RSpec 3.0, the example is instead exposed via
# an arg yielded to `it`, `before`, `let`, etc. However,
# some extension gems (such as Capybara) depend on the
# RSpec 2.x's `example` method, so this config option
# can be used to maintain compatibility.
#
# @param method_name [Symbol] the name of the helper method
#
# @example
#
# RSpec.configure do |rspec|
# rspec.expose_current_running_example_as :example
# end
#
# describe MyClass do
# before do
# # `example` can be used here because of the above config.
# do_something if example.metadata[:type] == "foo"
# end
# end
def expose_current_running_example_as(method_name)
ExposeCurrentExample.module_eval do
extend RSpec::SharedContext
let(method_name) { |ex| ex }
end

include ExposeCurrentExample
end

module ExposeCurrentExample; end

private

def get_files_to_run(paths)
Expand Down
22 changes: 22 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1436,5 +1436,27 @@ def strategy.order(list)
end
end

describe "#expose_current_running_example_as" do
before { stub_const(Configuration::ExposeCurrentExample.name, Module.new) }

it 'exposes the current example via the named method' do
RSpec.configuration.expose_current_running_example_as :the_example
RSpec.configuration.expose_current_running_example_as :another_example_helper

value_1 = value_2 = nil

ExampleGroup.describe "Group" do
it "works" do
value_1 = the_example
value_2 = another_example_helper
end
end.run

expect(value_1).to be_an(RSpec::Core::Example)
expect(value_1.description).to eq("works")
expect(value_2).to be(value_1)
end
end

end
end