Skip to content

Add around scenario hook #84

Merged
merged 1 commit into from Dec 6, 2011
View
8 lib/spinach/hookable.rb
@@ -24,8 +24,8 @@ def hook(hook)
define_method hook do |&block|
add_hook(hook, &block)
end
- define_method "run_#{hook}" do |*args|
- run_hook(hook, *args)
+ define_method "run_#{hook}" do |*args, &block|
+ run_hook(hook, *args, &block)
end
end
end
@@ -50,9 +50,9 @@ def reset
# @param [String] name
# the hook's name
#
- def run_hook(name, *args)
+ def run_hook(name, *args, &block)
if callbacks = hooks[name.to_sym]
- callbacks.each{ |c| c.call(*args) }
+ callbacks.each{ |c| c.call(*args, &block) }
end
end
View
8 lib/spinach/hooks.rb
@@ -56,6 +56,14 @@ class Hooks
# end
hook :before_scenario
+ # Runs around every scenario
+ # @example
+ # Spinach.hooks.around_scenario do |scenario_data, &block|
+ # # feature_data is a hash of the parsed scenario data
+ # block.call
+ # end
+ hook :around_scenario
+
# Runs after every scenario
#
# @example
View
1 lib/spinach/reporter.rb
@@ -31,6 +31,7 @@ def bind
hooks.after_feature { |*args| after_feature_run(*args) }
hooks.on_undefined_feature { |*args| on_feature_not_found(*args) }
hooks.before_scenario { |*args| before_scenario_run(*args) }
+ hooks.around_scenario { |*args, &block| around_scenario_run(*args, &block) }
hooks.after_scenario { |*args| after_scenario_run(*args) }
hooks.on_successful_step { |*args| on_successful_step(*args) }
hooks.on_undefined_step { |*args| on_undefined_step(*args) }
View
17 lib/spinach/runner/scenario_runner.rb
@@ -44,17 +44,18 @@ def step_definitions
# @api public
def run
Spinach.hooks.run_before_scenario @scenario
+ Spinach.hooks.run_around_scenario @scenario do
+ steps.each do |step|
+ Spinach.hooks.run_before_step step
- steps.each do |step|
- Spinach.hooks.run_before_step step
+ if @exception
+ Spinach.hooks.run_on_skipped_step step
+ else
+ run_step(step)
+ end
- if @exception
- Spinach.hooks.run_on_skipped_step step
- else
- run_step(step)
+ Spinach.hooks.run_after_step step
end
-
- Spinach.hooks.run_after_step step
end
Spinach.hooks.run_after_scenario @scenario
!@exception
View
2 test/spinach/hooks_test.rb
@@ -7,7 +7,7 @@
describe "hooks" do
%w{before_run after_run before_feature after_feature on_undefined_feature
- before_scenario after_scenario before_step after_step on_successful_step
+ before_scenario around_scenario after_scenario before_step after_step on_successful_step
on_failed_step on_error_step on_undefined_step on_skipped_step}.each do |callback|
describe "#{callback}" do
it "responds to #{callback}" do
View
7 test/spinach/reporter_test.rb
@@ -81,6 +81,13 @@ module Spinach
Spinach.hooks.run_before_scenario(anything)
end
+ it "binds a callback around every scenario" do
+ @reporter.expects(:around_scenario_run)
+ Spinach.hooks.run_around_scenario(anything) do
+ yield
+ end
+ end
+
it "binds a callback after every scenario" do
@reporter.expects(:after_scenario_run)
Spinach.hooks.run_after_scenario
View
1 test/spinach/runner/scenario_runner_test.rb
@@ -39,6 +39,7 @@ class Runner
subject.stubs(:step_definitions).returns step_definitions = stub
Spinach.hooks.expects(:run_before_scenario).with(scenario).in_sequence(hooks)
+ Spinach.hooks.expects(:run_around_scenario).with(scenario).in_sequence(hooks).yields
Spinach.hooks.expects(:run_before_step).with(steps.first).in_sequence(hooks)
subject.expects(:run_step).with(steps.first)
Something went wrong with that request. Please try again.