Permalink
Browse files

Handle some edge cases with before(:all)

- run before(:all) once for every group in a tree as long as it or its
  descendants have examples to run
- this means that a top level group's before(:all) should run if it has
  no examples, but its children do
  • Loading branch information...
dchelimsky committed Jun 11, 2010
1 parent 2b6ba51 commit 2fbd421fdea1983fa53a955be67c570d293f3384
Showing with 40 additions and 8 deletions.
  1. +30 −0 features/hooks/before_and_after_hooks.feature
  2. +9 −7 lib/rspec/core/example_group.rb
  3. +1 −1 rspec-core.gemspec
@@ -197,6 +197,36 @@ Feature: before and after hooks
Then I should see "1 example, 0 failures"
Then I should see matching /outer before all\n.outer after all\n\n\n\nFinished/
+ Scenario: nested examples have access to state set in outer before(:all)
+ Given a file named "before_all_spec.rb" with:
+ """
+ describe "something" do
+ before :all do
+ @value = 123
+ end
+
+ describe "nested" do
+ it "access state set in before(:all)" do
+ @value.should eq(123)
+ end
+
+ describe "nested more deeply" do
+ it "access state set in before(:all)" do
+ @value.should eq(123)
+ end
+ end
+ end
+
+ describe "nested in parallel" do
+ it "access state set in before(:all)" do
+ @value.should == 123
+ end
+ end
+ end
+ """
+ When I run "rspec before_all_spec.rb"
+ Then I should see "3 examples, 0 failures"
+
Scenario: before/after all blocks have access to state
Given a file named "before_and_after_all_spec.rb" with:
"""
@@ -65,13 +65,17 @@ def self.it_should_behave_like(*names)
end
def self.examples
- @_examples ||= []
+ @examples ||= []
end
def self.filtered_examples
world.filtered_examples[self]
end
+ def self.descendent_filtered_examples
+ filtered_examples + children.collect{|c| c.descendent_filtered_examples}
+ end
+
def self.metadata
@metadata
end
@@ -132,14 +136,12 @@ def self.before_all_ivars
end
def self.eval_before_alls(running_example)
- return if filtered_examples.empty?
+ return if descendent_filtered_examples.empty?
superclass.before_all_ivars.each { |ivar, val| running_example.instance_variable_set(ivar, val) }
world.run_hook(:before, :all, self, running_example)
- ancestors.reverse.each do |ancestor|
- until ancestor.before_alls.empty?
- running_example.instance_eval &ancestor.before_alls.shift
- end
+ until before_alls.empty?
+ running_example.instance_eval &before_alls.shift
end
running_example.instance_variables.each { |ivar| before_all_ivars[ivar] = running_example.instance_variable_get(ivar) }
end
@@ -155,7 +157,7 @@ def self.eval_after_eachs(running_example)
end
def self.eval_after_alls(running_example)
- return if filtered_examples.empty?
+ return if descendent_filtered_examples.empty?
before_all_ivars.each { |ivar, val| running_example.instance_variable_set(ivar, val) }
ancestors.each do |ancestor|
until ancestor.after_alls.empty?
View
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["Chad Humphries", "David Chelimsky"]
- s.date = %q{2010-06-10}
+ s.date = %q{2010-06-11}
s.description = %q{RSpec runner and example group classes}
s.email = %q{dchelimsky@gmail.com;chad.humphries@gmail.com}
s.executables = ["rspec", "spec"]

0 comments on commit 2fbd421

Please sign in to comment.