Skip to content

Commit

Permalink
Add expose_current_running_example_as config option.
Browse files Browse the repository at this point in the history
Fixes #963.
  • Loading branch information
myronmarston committed Nov 1, 2013
1 parent a7dcd1d commit 98c6fa8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/rspec/core/configuration.rb
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
15 changes: 15 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Expand Up @@ -1436,5 +1436,20 @@ def strategy.order(list)
end
end

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

value = nil

ExampleGroup.describe "Group" do
it("works") { value = the_example }
end.run

expect(value).to be_an(RSpec::Core::Example)
expect(value.description).to eq("works")
end
end

end
end

0 comments on commit 98c6fa8

Please sign in to comment.