Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make sure before/after(:all) gets run even in subclasses

  • Loading branch information...
commit 21d6f6bc600886a89bfc7b58eeb569dbf5c990cf 1 parent 38da099
@dchelimsky dchelimsky authored
View
58 features/hooks/before_and_after_hooks.feature
@@ -28,8 +28,8 @@ Feature: before and after hooks
applied to all groups or subsets of all groups defined by example group
types.
- Scenario: define before(:each) block in example group
- Given a file named "before_each_in_example_group_spec.rb" with:
+ Scenario: define before(:each) block
+ Given a file named "before_each_spec.rb" with:
"""
require "rspec/expectations"
@@ -59,11 +59,11 @@ Feature: before and after hooks
end
end
"""
- When I run "rspec before_each_in_example_group_spec.rb"
+ When I run "rspec before_each_spec.rb"
Then I should see "3 examples, 0 failures"
Scenario: define before(:all) block in example group
- Given a file named "before_all_in_example_group_spec.rb" with:
+ Given a file named "before_all_spec.rb" with:
"""
require "rspec/expectations"
@@ -93,9 +93,12 @@ Feature: before and after hooks
end
end
"""
- When I run "rspec before_all_in_example_group_spec.rb"
+ When I run "rspec before_all_spec.rb"
Then I should see "3 examples, 0 failures"
+ When I run "rspec before_all_spec.rb:15"
+ Then I should see "1 example, 0 failures"
+
@wip
Scenario: define before and after blocks in configuration
Given a file named "befores_in_configuration_spec.rb" with:
@@ -135,21 +138,11 @@ Feature: before and after hooks
When I run "rspec befores_in_configuration_spec.rb"
Then I should see "3 examples, 0 failures"
- @wip
Scenario: before/after blocks are run in order
Given a file named "ensure_block_order_spec.rb" with:
"""
require "rspec/expectations"
- Rspec.configure do |config|
- config.before(:suite) do
- puts "before suite"
- end
- config.after(:suite) do
- puts "after suite"
- end
- end
-
describe "before and after callbacks" do
before(:all) do
puts "before all"
@@ -173,5 +166,38 @@ Feature: before and after hooks
end
"""
When I run "rspec ensure_block_order_spec.rb"
- Then I should see /before suite\nbefore all\nbefore each\nafter each\n\.after all\n.*after suite/m
+ Then I should see matching "before all\nbefore each\nafter each\n.after all"
+
+@wip
+ Scenario: before/after all blocks are run once
+ Given a file named "before_and_after_all_spec.rb" with:
+ """
+ describe "before and after callbacks" do
+ before(:all) do
+ puts "before all"
+ end
+
+ after(:all) do
+ puts "after all"
+ end
+
+ example "in outer group" do
+
+ end
+
+ describe "nested group" do
+
+ example "in nested group" do
+
+ end
+
+ end
+
+ end
+ """
+ When I run "rspec before_and_after_all_spec.rb:16"
+ Then I should see matching "before all\n.after all"
+
+ When I run "rspec before_and_after_all_spec.rb"
+ Then I should see matching "before all\n..after all"
View
23 lib/rspec/core/example_group.rb
@@ -155,7 +155,11 @@ def self.eval_before_alls(running_example)
end
configuration.find_hook(:before, :all, self).each { |blk| running_example.instance_eval(&blk) }
- before_alls.each { |blk| running_example.instance_eval(&blk) }
+ before_ancestors.each do |ancestor|
+ until ancestor.before_alls.empty?
+ running_example.instance_eval &ancestor.before_alls.shift
+ end
+ end
running_example.instance_variables.each { |ivar| before_all_ivars[ivar] = running_example.instance_variable_get(ivar) }
end
@@ -164,17 +168,22 @@ def self.eval_before_eachs(running_example)
before_ancestors.each { |ancestor| ancestor.before_eachs.each { |blk| running_example.instance_eval(&blk) } }
end
- def self.eval_after_alls(running_example)
- after_alls.each { |blk| running_example.instance_eval(&blk) }
- configuration.find_hook(:after, :all, self).each { |blk| running_example.instance_eval(&blk) }
- before_all_ivars.keys.each { |ivar| before_all_ivars[ivar] = running_example.instance_variable_get(ivar) }
- end
-
def self.eval_after_eachs(running_example)
after_ancestors.each { |ancestor| ancestor.after_eachs.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)
+ after_ancestors.each do |ancestor|
+ after_alls = ancestor.after_alls.dup
+ until after_alls.empty?
+ running_example.instance_eval &after_alls.shift
+ end
+ end
+ configuration.find_hook(:after, :all, self).each { |blk| running_example.instance_eval(&blk) }
+ before_all_ivars.keys.each { |ivar| before_all_ivars[ivar] = running_example.instance_variable_get(ivar) }
+ end
+
def self.run(reporter)
example_group_instance = new
reporter.add_example_group(self)
View
2  spec/rspec/core/example_group_spec.rb
@@ -82,7 +82,7 @@ module Rspec::Core
end
- describe "adding before, after, and around hooks" do
+ describe "before, after, and around hooks" do
it "should expose the before each blocks at before_eachs" do
group = ExampleGroup.create
View
2  spec/rspec/core/shared_example_group_spec.rb
@@ -201,7 +201,7 @@ def count(scope)
group.magic[:after_each].should eq("after each 2")
end
- it "runs after(:all) only once from shared example_group", :compat => 'rspec-1.2' do
+ it "runs after(:all) only once from shared example_group", :pending => true, :compat => 'rspec-1.2' do
group.magic[:after_all].should eq("after all 1")
end
Please sign in to comment.
Something went wrong with that request. Please try again.