Skip to content
Browse files

Add before/after(:suite) hooks to configuration

Closes #43.
  • Loading branch information...
1 parent 42b6846 commit f9bcb44e68a96be9c4997d8c11bcf8740ae29830 @dchelimsky dchelimsky committed Jun 17, 2010
View
70 features/hooks/before_and_after_hooks.feature
@@ -14,10 +14,12 @@ Feature: before and after hooks
after(:all) blocks are run once after all of the examples in a group
Before and after blocks are called in the following order:
+ before suite
before all
before each
- after each
- after all
+ after each
+ after all
+ after suite
Before and after blocks can be defined in the example groups to which they
apply or in a configuration. When defined in a configuration, they can be
@@ -155,6 +157,53 @@ Feature: before and after hooks
When I run "rspec ./ensure_block_order_spec.rb"
Then I should see matching /before all\nbefore each\nafter each\n.after all/
+ Scenario: before/after blocks defined in config are run in order
+ Given a file named "configuration_spec.rb" with:
+ """
+ require "rspec/expectations"
+
+ RSpec.configure do |config|
+ config.before(:suite) do
+ puts "before suite"
+ end
+
+ config.before(:all) do
+ puts "before all"
+ end
+
+ config.before(:each) do
+ puts "before each"
+ end
+
+ config.after(:each) do
+ puts "after each"
+ end
+
+ config.after(:all) do
+ puts "after all"
+ end
+
+ config.after(:suite) do
+ puts "after suite"
+ end
+ end
+
+ describe "ignore" do
+ example "ignore" do
+ end
+ end
+ """
+ When I run "rspec configuration_spec.rb"
+ Then I should see matching:
+ """
+ before suite
+ before all
+ before each
+ after each
+ .after all
+ after suite
+ """
+
Scenario: before/after all blocks are run once
Given a file named "before_and_after_all_spec.rb" with:
"""
@@ -278,20 +327,3 @@ Feature: before and after hooks
When I run "rspec ./error_in_before_each_spec.rb"
Then I should see "1 example, 1 failure"
And I should see "this error"
-
-@wip
- Scenario: exception in before(:all) is captured and reported as failure
- Given a file named "error_in_before_all_spec.rb" with:
- """
- describe "error in before(:all)" do
- before(:all) do
- raise "this error"
- end
-
- it "is reported as failure" do
- end
- end
- """
- When I run "rspec ./error_in_before_all_spec.rb"
- Then I should see "1 example, 1 failure"
- And I should see "this error"
View
9 lib/rspec/core/command_line.rb
@@ -19,9 +19,14 @@ def run(err, out)
world.announce_inclusion_filter
configuration.reporter.report(example_count) do |reporter|
- example_groups.run_examples(reporter)
+ begin
+ configuration.run_before_suite
+ example_groups.run_examples(reporter)
+ ensure
+ configuration.run_after_suite
+ end
end
-
+
example_groups.success?
end
View
16 lib/rspec/core/configuration.rb
@@ -246,12 +246,20 @@ def find_modules(group)
end
end
- def before(each_or_all=:each, options={}, &block)
- hooks[:before][each_or_all] << [options, block]
+ def before(scope=:each, options={}, &block)
+ hooks[:before][scope] << [options, block]
end
- def after(each_or_all=:each, options={}, &block)
- hooks[:after][each_or_all] << [options, block]
+ def after(scope=:each, options={}, &block)
+ hooks[:after][scope] << [options, block]
+ end
+
+ def run_before_suite
+ hooks[:before][:suite].each {|hook| hook.last.call}
+ end
+
+ def run_after_suite
+ hooks[:after][:suite].each {|hook| hook.last.call}
end
def find_hook(hook, each_or_all, group)
View
40 spec/rspec/core/command_line_spec.rb
@@ -17,5 +17,45 @@ module RSpec::Core
command_line.instance_eval { @options }.should be(config_options)
end
end
+
+ describe "#run" do
+ let(:config_options) do
+ config_options = ConfigurationOptions.new(%w[--color])
+ config_options.parse_options
+ config_options
+ end
+
+ let(:command_line) do
+ CommandLine.new(config_options)
+ end
+
+ it "runs before suite hooks" do
+ err = out = StringIO.new
+ config = RSpec::Core::Configuration.new
+ config.should_receive(:run_before_suite)
+ command_line.stub(:configuration) { config }
+ command_line.run(err, out)
+ end
+
+ it "runs after suite hooks" do
+ err = out = StringIO.new
+ config = RSpec::Core::Configuration.new
+ config.should_receive(:run_after_suite)
+ command_line.stub(:configuration) { config }
+ command_line.run(err, out)
+ end
+
+ it "runs after suite hooks even after an error" do
+ err = out = StringIO.new
+ config = RSpec::Core::Configuration.new
+ config.stub(:run_before_suite) { raise "this error" }
+ config.should_receive(:run_after_suite)
+ command_line.stub(:configuration) { config }
+ expect do
+ command_line.run(err, out)
+ end.to raise_error
+ end
+ end
+
end
end
View
21 spec/rspec/core/configuration_spec.rb
@@ -402,5 +402,26 @@ def that_thing
end
end
+ describe "#run_before_suite" do
+ it "runs any before suite hooks" do
+ calls = 0
+ config.before(:suite) { calls += 1 }
+ config.before(:suite) { calls += 1 }
+ config.before(:suite) { calls += 1 }
+ config.run_before_suite
+ calls.should == 3
+ end
+ end
+
+ describe "#run_after_suite" do
+ it "runs any after suite hooks" do
+ calls = 0
+ config.after(:suite) { calls += 1 }
+ config.after(:suite) { calls += 1 }
+ config.after(:suite) { calls += 1 }
+ config.run_after_suite
+ calls.should == 3
+ end
+ end
end
end

0 comments on commit f9bcb44

Please sign in to comment.
Something went wrong with that request. Please try again.