Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.
53 changes: 53 additions & 0 deletions features/docs/writing_support_code/after_step_hooks.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Feature: AfterStep Hooks
AfterStep hooks can be used to further inspect the Step object of the step
that has just run, or to simply check the step's result.

Background:
Given the standard step definitions
And a file named "features/sample.feature" with:
"""
Feature: Sample

Scenario: Success
Given this step passes
"""

Scenario: Access Test Step object in AfterStep Block
Given a file named "features/support/env.rb" with:
"""
AfterStep do |result, test_step|
expect(test_step).to be_a(Cucumber::Core::Test::Step)
end
"""
When I run `cucumber features`
Then it should pass with:
"""
Feature: Sample

Scenario: Success # features/sample.feature:3
Given this step passes # features/step_definitions/steps.rb:1

1 scenario (1 passed)
1 step (1 passed)

"""

Scenario: An AfterStep with one named argument receives only the result
Given a file named "features/support/env.rb" with:
"""
AfterStep do |result|
expect(result).to be_a(Cucumber::Core::Test::Result::Passed)
end
"""
When I run `cucumber features`
Then it should pass with:
"""
Feature: Sample

Scenario: Success # features/sample.feature:3
Given this step passes # features/step_definitions/steps.rb:1

1 scenario (1 passed)
1 step (1 passed)

"""
2 changes: 1 addition & 1 deletion lib/cucumber/rb_support/rb_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(rb_language, tag_expressions, proc)
end

def invoke(pseudo_method, arguments, &block)
@rb_language.current_world.cucumber_instance_exec(false, pseudo_method, *[arguments, block].compact, &@proc)
@rb_language.current_world.cucumber_instance_exec(false, pseudo_method, *[arguments, block].flatten.compact, &@proc)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cucumber/runtime/step_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def apply(test_steps)
private
def after_step_hooks(test_step)
@hooks.map do |hook|
action = ->(*args) { hook.invoke('AfterStep', args) }
action = ->(*args) { hook.invoke('AfterStep', [args, test_step]) }
Hooks.after_step_hook(test_step.source, hook.location, &action)
end
end
Expand Down