Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Expose example.metadata in around hooks

- Closes #221.
  • Loading branch information...
commit b6a7897ef7e105a77984db2a0ece81cf967678a2 1 parent 2466831
@dchelimsky dchelimsky authored
View
20 features/hooks/around_hooks.feature
@@ -9,7 +9,7 @@ Feature: around hooks
example, if your database library offers a transaction method that receives
a block, you can use an around hook as described in the first scenario:
- Scenario: use the example as a block within the block passed to around()
+ Scenario: use the example as a proc within the block passed to around()
Given a file named "example_spec.rb" with:
"""
class Database
@@ -41,7 +41,7 @@ Feature: around hooks
Scenario: invoke the example using run()
Given a file named "example_spec.rb" with:
"""
- describe "around filter" do
+ describe "around hook" do
around(:each) do |example|
puts "around each before"
example.run
@@ -61,6 +61,22 @@ Feature: around hooks
around each after
"""
+ Scenario: access the example metadata
+ Given a file named "example_spec.rb" with:
+ """
+ describe "something" do
+ around(:each) do |example|
+ puts example.metadata[:foo]
+ example.run
+ end
+
+ it "does something", :foo => "this should show up in the output" do
+ end
+ end
+ """
+ When I run "rspec example_spec.rb"
+ Then the output should contain "this should show up in the output"
+
Scenario: define a global around hook
Given a file named "example_spec.rb" with:
"""
View
10 lib/rspec/core/example.rb
@@ -66,6 +66,14 @@ def fail_fast(reporter, exception)
finish(reporter)
end
+ class Procsy < Proc
+ attr_reader :metadata
+ alias_method :run, :call
+ def initialize(metadata)
+ @metadata = metadata

I traced the JRuby problem down to not calling super here. That relies on an extremely subtle behavior of MRI's Proc -- it allows binding to the implicit block in the current context, even if the subclass's #initialize method doesn't call super.

I suggest adding super here -- it both makes the code clearer and allows around hooks to work on JRuby again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+ end
+
private
def with_pending_capture
@@ -79,7 +87,7 @@ def with_around_hooks
if @example_group_class.around_hooks.empty?
yield
else
- @example_group_class.eval_around_eachs(@example_group_instance, Proc.new).call
+ @example_group_class.eval_around_eachs(@example_group_instance, Procsy.new(metadata)).call
end
end
View
9 lib/rspec/core/example_group.rb
@@ -177,10 +177,11 @@ def self.eval_before_alls(example_group_instance)
store_before_all_ivars(example_group_instance)
end
- def self.eval_around_eachs(example_group_instance, wrapped_example)
- around_hooks.reverse.inject(wrapped_example) do |wrapper, hook|
- def wrapper.run; call; end
- lambda { example_group_instance.instance_eval_with_args(wrapper, &hook) }
+ def self.eval_around_eachs(example_group_instance, procsy)
+ around_hooks.reverse.inject(procsy) do |procsy, around_hook|
+ Example::Procsy.new(procsy.metadata) do
+ example_group_instance.instance_eval_with_args(procsy, &around_hook)
+ end
end
end

1 comment on commit b6a7897

@jfirebaugh

FYI, this commit exposed a JRuby bug that causes any spec with an around hook to fail:

http://jira.codehaus.org/browse/JRUBY-5261

Please sign in to comment.
Something went wrong with that request. Please try again.