Permalink
Browse files

Ensure befores and afters are executed in the correct order.

  • Loading branch information...
1 parent 17a3b5a commit 98909a2c83299fd2e48c40cef1c508fe5620ff10 @dchelimsky dchelimsky committed May 7, 2010
Showing with 56 additions and 53 deletions.
  1. +4 −5 lib/rspec/core/example_group.rb
  2. +52 −48 spec/rspec/core/example_group_spec.rb
@@ -67,7 +67,7 @@ def self.examples
@_examples ||= []
end
- def self.filtered_examples
+ def self.filtered_examples(fresh = false)
examples = self.examples.dup
examples = world.apply_exclusion_filters(examples, world.exclusion_filter) if world.exclusion_filter
examples = world.apply_inclusion_filters(examples, world.inclusion_filter) if world.inclusion_filter
@@ -168,16 +168,15 @@ def self.eval_before_eachs(running_example)
end
def self.eval_after_eachs(running_example)
- ancestors.each { |ancestor| ancestor.after_eachs.each { |blk| running_example.instance_eval(&blk) } }
+ ancestors.each { |ancestor| ancestor.after_eachs.reverse.each { |blk| running_example.instance_eval(&blk) } }
configuration.find_hook(:after, :each, self).each { |blk| running_example.instance_eval(&blk) }
end
def self.eval_after_alls(running_example)
return if filtered_examples.empty?
ancestors.each do |ancestor|
- after_alls = ancestor.after_alls
- until after_alls.empty?
- running_example.instance_eval &after_alls.shift
+ until ancestor.after_alls.empty?
+ running_example.instance_eval &ancestor.after_alls.pop
end
end
configuration.find_hook(:after, :all, self).each { |blk| running_example.instance_eval(&blk) }
@@ -166,72 +166,76 @@ module Rspec::Core
describe "before, after, and around hooks" do
- it "exposes the before each blocks at before_eachs" do
+ it "runs the before alls in order" do
group = ExampleGroup.describe
- group.before(:each) { 'foo' }
- group.should have(1).before_eachs
- end
+ order = []
+ group.before(:all) { order << 1 }
+ group.before(:all) { order << 2 }
+ group.before(:all) { order << 3 }
+ group.example("example") {}
- it "maintains the before each block order" do
- group = ExampleGroup.describe
- group.before(:each) { 15 }
- group.before(:each) { 'A' }
- group.before(:each) { 33.5 }
+ group.run_all
- group.before_eachs[0].call.should == 15
- group.before_eachs[1].call.should == 'A'
- group.before_eachs[2].call.should == 33.5
+ order.should == [1,2,3]
end
- it "exposes the before all blocks at before_alls" do
+ it "runs the before eachs in order" do
group = ExampleGroup.describe
- group.before(:all) { 'foo' }
- group.should have(1).before_alls
- end
+ order = []
+ group.before(:each) { order << 1 }
+ group.before(:each) { order << 2 }
+ group.before(:each) { order << 3 }
+ group.example("example") {}
- it "maintains the before all block order" do
- group = ExampleGroup.describe
- group.before(:all) { 15 }
- group.before(:all) { 'A' }
- group.before(:all) { 33.5 }
+ group.run_all
- group.before_alls[0].call.should == 15
- group.before_alls[1].call.should == 'A'
- group.before_alls[2].call.should == 33.5
+ order.should == [1,2,3]
end
- it "exposes the after each blocks at after_eachs" do
+ it "runs the after eachs in reverse order" do
group = ExampleGroup.describe
- group.after(:each) { 'foo' }
- group.should have(1).after_eachs
- end
+ order = []
+ group.after(:each) { order << 1 }
+ group.after(:each) { order << 2 }
+ group.after(:each) { order << 3 }
+ group.example("example") {}
- it "maintains the after each block order" do
- group = ExampleGroup.describe
- group.after(:each) { 15 }
- group.after(:each) { 'A' }
- group.after(:each) { 33.5 }
+ group.run_all
- group.after_eachs[0].call.should == 15
- group.after_eachs[1].call.should == 'A'
- group.after_eachs[2].call.should == 33.5
+ order.should == [3,2,1]
end
- it "exposes the after all blocks at after_alls" do
+ it "runs the after alls in reverse order" do
group = ExampleGroup.describe
- group.after(:all) { 'foo' }
- group.should have(1).after_alls
+ order = []
+ group.after(:all) { order << 1 }
+ group.after(:all) { order << 2 }
+ group.after(:all) { order << 3 }
+ group.example("example") {}
+
+ group.run_all
+
+ order.should == [3,2,1]
end
- it "maintains the after each block order" do
+ it "runs before all, before each, example, after each, after all, in that order" do
group = ExampleGroup.describe
- group.after(:all) { 15 }
- group.after(:all) { 'A' }
- group.after(:all) { 33.5 }
-
- group.after_alls[0].call.should == 15
- group.after_alls[1].call.should == 'A'
- group.after_alls[2].call.should == 33.5
+ order = []
+ group.after(:all) { order << :after_all }
+ group.after(:each) { order << :after_each }
+ group.before(:each) { order << :before_each }
+ group.before(:all) { order << :before_all }
+ group.example("example") { order << :example }
+
+ group.run_all
+
+ order.should == [
+ :before_all,
+ :before_each,
+ :example,
+ :after_each,
+ :after_all
+ ]
end
it "exposes the around each blocks at after_alls" do
@@ -255,7 +259,7 @@ module Rspec::Core
group.it("should do something 1") { }
group.it("should do something 2") { }
group.it("should do something 3") { }
- group.examples.size.should == 3
+ group.should have(3).examples
end
it "maintains the example order" do

0 comments on commit 98909a2

Please sign in to comment.