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

Commit d941ba7

Browse files
committed
yield current running example to it/example and before/after hooks
1 parent dee12fc commit d941ba7

File tree

6 files changed

+63
-67
lines changed

6 files changed

+63
-67
lines changed

lib/rspec/core/example.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def run(example_group_instance, reporter)
111111
with_around_each_hooks do
112112
begin
113113
run_before_each
114-
@example_group_instance.instance_eval(&@example_block)
114+
@example_group_instance.instance_exec(self, &@example_block)
115115
rescue Pending::PendingDeclaredInExample => e
116116
@pending_declared_in_example = e.message
117117
rescue Exception => e
@@ -239,7 +239,7 @@ def instance_eval(*args, &block)
239239

240240
# @private
241241
def instance_eval_with_rescue(context = nil, &block)
242-
@example_group_instance.instance_eval_with_rescue(context, &block)
242+
@example_group_instance.instance_eval_with_rescue(self, context, &block)
243243
end
244244

245245
# @private

lib/rspec/core/example_group.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -439,16 +439,24 @@ def self.set_ivars(instance, ivars)
439439
ivars.each {|name, value| instance.instance_variable_set(name, value)}
440440
end
441441

442-
# @attr_reader
443-
# Returns the {Example} object that wraps this instance of
444-
# `ExampleGroup`
445-
attr_accessor :example
442+
def initialize
443+
@_current_rspec_example = nil
444+
end
445+
446+
def example=(current_example)
447+
@_current_rspec_example = current_example
448+
end
449+
450+
# @deprecated use a block argument
451+
def example
452+
RSpec.deprecate("example", :replacement => "a block argument")
453+
@_current_rspec_example
454+
end
446455

447-
# @deprecated use {ExampleGroup#example}
456+
# @deprecated use a block argument
448457
def running_example
449-
RSpec.deprecate("running_example",
450-
:replacement => "example")
451-
example
458+
RSpec.deprecate("running_example", :replacement => "a block argument")
459+
@_current_rspec_example
452460
end
453461

454462
# Returns the class or module passed to the `describe` method (or alias).
@@ -468,12 +476,12 @@ def described_class
468476
# @private
469477
# instance_evals the block, capturing and reporting an exception if
470478
# raised
471-
def instance_eval_with_rescue(context = nil, &hook)
479+
def instance_eval_with_rescue(example, context = nil, &hook)
472480
begin
473-
instance_eval(&hook)
481+
instance_exec(example, &hook)
474482
rescue Exception => e
475-
raise unless example
476-
example.set_exception(e, context)
483+
raise unless @_current_rspec_example
484+
@_current_rspec_example.set_exception(e, context)
477485
end
478486
end
479487
end

lib/rspec/core/hooks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def options_apply?(example_or_group)
1818

1919
class BeforeHook < Hook
2020
def run(example)
21-
example.instance_eval(&block)
21+
example.instance_exec(example, &block)
2222
end
2323

2424
def display_name

lib/rspec/core/pending.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def pending_fixed?; true; end
7676
# # ...
7777
# end
7878
def pending(*args)
79-
return self.class.before(:each) { pending(*args) } unless example
79+
return self.class.before(:each) { pending(*args) } unless @_current_rspec_example
8080

8181
options = args.last.is_a?(Hash) ? args.pop : {}
8282
message = args.first || NO_REASON_GIVEN
@@ -85,18 +85,17 @@ def pending(*args)
8585
return block_given? ? yield : nil
8686
end
8787

88-
example.metadata[:pending] = true
89-
example.metadata[:execution_result][:pending_message] = message
88+
@_current_rspec_example.metadata[:pending] = true
89+
@_current_rspec_example.metadata[:execution_result][:pending_message] = message
9090
if block_given?
9191
begin
9292
result = begin
9393
yield
94-
example.example_group_instance.instance_eval { verify_mocks_for_rspec }
95-
true
94+
@_current_rspec_example.example_group_instance.instance_eval { verify_mocks_for_rspec }
9695
end
97-
example.metadata[:pending] = false
96+
@_current_rspec_example.metadata[:pending] = false
9897
rescue Exception => e
99-
example.execution_result[:exception] = e
98+
@_current_rspec_example.execution_result[:exception] = e
10099
ensure
101100
teardown_mocks_for_rspec
102101
end

spec/rspec/core/example_group_spec.rb

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def metadata_hash(*args)
6565
examples_run = []
6666
group = ExampleGroup.describe("parent") do
6767
describe("child") do
68-
it "does something" do
69-
examples_run << example
68+
it "does something" do |ex|
69+
examples_run << ex
7070
end
7171
end
7272
end
@@ -79,13 +79,13 @@ def metadata_hash(*args)
7979
it "runs its children " do
8080
examples_run = []
8181
group = ExampleGroup.describe("parent") do
82-
it "fails" do
83-
examples_run << example
82+
it "fails" do |ex|
83+
examples_run << ex
8484
raise "fail"
8585
end
8686
describe("child") do
87-
it "does something" do
88-
examples_run << example
87+
it "does something" do |ex|
88+
examples_run << ex
8989
end
9090
end
9191
end
@@ -663,19 +663,10 @@ def define_and_run_group(define_outer_example = false)
663663
end
664664
end
665665

666-
it "has no 'running example' within before(:all)" do
667-
group = ExampleGroup.describe
668-
running_example = :none
669-
group.before(:all) { running_example = example }
670-
group.example("no-op") { }
671-
group.run
672-
expect(running_example).to be(nil)
673-
end
674-
675666
it "has access to example options within before(:each)" do
676667
group = ExampleGroup.describe
677668
option = nil
678-
group.before(:each) { option = example.options[:data] }
669+
group.before(:each) {|ex| option = ex.options[:data] }
679670
group.example("no-op", :data => :sample) { }
680671
group.run
681672
expect(option).to eq(:sample)
@@ -684,20 +675,11 @@ def define_and_run_group(define_outer_example = false)
684675
it "has access to example options within after(:each)" do
685676
group = ExampleGroup.describe
686677
option = nil
687-
group.after(:each) { option = example.options[:data] }
678+
group.after(:each) {|ex| option = ex.options[:data] }
688679
group.example("no-op", :data => :sample) { }
689680
group.run
690681
expect(option).to eq(:sample)
691682
end
692-
693-
it "has no 'running example' within after(:all)" do
694-
group = ExampleGroup.describe
695-
running_example = :none
696-
group.after(:all) { running_example = example }
697-
group.example("no-op") { }
698-
group.run
699-
expect(running_example).to be(nil)
700-
end
701683
end
702684

703685
%w[pending xit xspecify xexample].each do |method_name|
@@ -755,20 +737,20 @@ def define_and_run_group(define_outer_example = false)
755737
describe Object, "describing nested example_groups", :little_less_nested => 'yep' do
756738

757739
describe "A sample nested group", :nested_describe => "yep" do
758-
it "sets the described class to the described class of the outer most group" do
759-
expect(example.example_group.described_class).to eq(ExampleGroup)
740+
it "sets the described class to the described class of the outer most group" do |ex|
741+
expect(ex.example_group.described_class).to eq(ExampleGroup)
760742
end
761743

762-
it "sets the description to 'A sample nested describe'" do
763-
expect(example.example_group.description).to eq('A sample nested group')
744+
it "sets the description to 'A sample nested describe'" do |ex|
745+
expect(ex.example_group.description).to eq('A sample nested group')
764746
end
765747

766-
it "has top level metadata from the example_group and its parent groups" do
767-
expect(example.example_group.metadata).to include(:little_less_nested => 'yep', :nested_describe => 'yep')
748+
it "has top level metadata from the example_group and its parent groups" do |ex|
749+
expect(ex.example_group.metadata).to include(:little_less_nested => 'yep', :nested_describe => 'yep')
768750
end
769751

770-
it "exposes the parent metadata to the contained examples" do
771-
expect(example.metadata).to include(:little_less_nested => 'yep', :nested_describe => 'yep')
752+
it "exposes the parent metadata to the contained examples" do |ex|
753+
expect(ex.metadata).to include(:little_less_nested => 'yep', :nested_describe => 'yep')
772754
end
773755
end
774756

@@ -826,12 +808,12 @@ def define_and_run_group(define_outer_example = false)
826808
expect(@before_all_top_level).to eq('before_all_top_level')
827809
end
828810

829-
it "can access the before all ivars in the before_all_ivars hash", :ruby => 1.8 do
830-
expect(example.example_group.before_all_ivars).to include('@before_all_top_level' => 'before_all_top_level')
811+
it "can access the before all ivars in the before_all_ivars hash", :ruby => 1.8 do |ex|
812+
expect(ex.example_group.before_all_ivars).to include('@before_all_top_level' => 'before_all_top_level')
831813
end
832814

833-
it "can access the before all ivars in the before_all_ivars hash", :ruby => 1.9 do
834-
expect(example.example_group.before_all_ivars).to include(:@before_all_top_level => 'before_all_top_level')
815+
it "can access the before all ivars in the before_all_ivars hash", :ruby => 1.9 do |ex|
816+
expect(ex.example_group.before_all_ivars).to include(:@before_all_top_level => 'before_all_top_level')
835817
end
836818

837819
describe "but now I am nested" do

spec/rspec/core/example_spec.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,19 @@ def assert(val)
171171
end
172172

173173
describe "accessing metadata within a running example" do
174-
it "has a reference to itself when running" do
175-
expect(example.description).to eq("has a reference to itself when running")
174+
it "has a reference to itself when running" do |ex|
175+
expect(ex.description).to eq("has a reference to itself when running")
176176
end
177177

178-
it "can access the example group's top level metadata as if it were its own" do
179-
expect(example.example_group.metadata).to include(:parent_metadata => 'sample')
180-
expect(example.metadata).to include(:parent_metadata => 'sample')
178+
it "can access the example group's top level metadata as if it were its own" do |ex|
179+
expect(ex.example_group.metadata).to include(:parent_metadata => 'sample')
180+
expect(ex.metadata).to include(:parent_metadata => 'sample')
181181
end
182182
end
183183

184184
describe "accessing options within a running example" do
185-
it "can look up option values by key", :demo => :data do
186-
expect(example.metadata[:demo]).to eq(:data)
185+
it "can look up option values by key", :demo => :data do |ex|
186+
expect(ex.metadata[:demo]).to eq(:data)
187187
end
188188
end
189189

@@ -436,4 +436,11 @@ def run_and_capture_reported_message(group)
436436

437437
expect(values.uniq).to have(2).values
438438
end
439+
440+
describe "optional block argument" do
441+
it "contains the example" do |ex|
442+
expect(ex).to be_an(RSpec::Core::Example)
443+
expect(ex.description).to match(/contains the example/)
444+
end
445+
end
439446
end

0 commit comments

Comments
 (0)