Permalink
Browse files

Ensure before(:all) state is always propagated to after(:all)

...even if an error occurs in before(:all).

Fixes #558.
  • Loading branch information...
1 parent 824119e commit 49370db129736dd406251b54a528367ea1309d0d @samphippen samphippen committed with myronmarston Sep 25, 2012
Showing with 27 additions and 3 deletions.
  1. +3 −0 Changelog.md
  2. +6 −3 lib/rspec/core/example_group.rb
  3. +18 −0 spec/rspec/core/example_group_spec.rb
View
@@ -28,6 +28,9 @@ Bug fixes
`before(:all) hook` (Bradley Schaefer).
* Make the order spec files are loaded consistent, regardless of the
order of the files returned by the OS (Jo Liss).
+* Ensure instance variables from `before(:all)` are always exposed
+ from `after(:all)`, even if an error occurs in `before(:all)`
+ (Sam Phippen).
### 2.11.1 / 2012-07-18
[full changelog](http://github.com/rspec/rspec-core/compare/v2.11.0...v2.11.1)
@@ -309,9 +309,12 @@ def self.assign_before_all_ivars(ivars, example_group_instance)
# @private
def self.run_before_all_hooks(example_group_instance)
return if descendant_filtered_examples.empty?
- assign_before_all_ivars(superclass.before_all_ivars, example_group_instance)
- run_hook(:before, :all, example_group_instance)
- store_before_all_ivars(example_group_instance)
+ begin
+ assign_before_all_ivars(superclass.before_all_ivars, example_group_instance)
+ run_hook(:before, :all, example_group_instance)
+ ensure
+ store_before_all_ivars(example_group_instance)
+ end
end
# @private
@@ -562,6 +562,24 @@ def metadata_hash(*args)
example.metadata[:execution_result][:exception].message.should eq("error in before all")
end
+ it "exposes instance variables set in before(:all) from after(:all) even if a before(:all) error occurs" do
+ ivar_value_in_after_hook = nil
+
+ group = ExampleGroup.describe do
+ before(:all) do
+ @an_ivar = :set_in_before_all
+ raise "fail"
+ end
+
+ after(:all) { ivar_value_in_after_hook = @an_ivar }
+
+ it("has a spec") { }
+ end
+
+ group.run
+ ivar_value_in_after_hook.should eq(:set_in_before_all)
+ end
+
it "treats an error in before(:all) as a failure for a spec in a nested group" do
example = nil
group = ExampleGroup.describe do

0 comments on commit 49370db

Please sign in to comment.