Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Expose names of AST elements in Before hook #430

Open
wants to merge 1 commit into from

3 participants

Matt Wynne Oleg Sukhodolsky Chris Parsons
Matt Wynne
Owner

See vcr/vcr#288 and #425.

We should make this part of the public API.

features/before_hook.feature
((7 lines not shown))
+
+ Scenario: Scenario name
+ Given a step
+
+ Scenario Outline: Scenario Outline name
+ Given a step
+
+ Examples: Examples Table name
+ | row |
+ """
+ And a file named "features/support/hook.rb" with:
+ """
+ names = []
+ Before do |scenario|
+ names << scenario.feature_name
+ if scenario.respond_to?(:scenario_name)
Matt Wynne Owner

@tooky @os97673 can you see a way to avoid having to use this if statement? Something using a null object / blocks?

Oleg Sukhodolsky Collaborator
os97673 added a note

@mattwynne the question is what you want to fix here, what functionality want to provide?
The code in question looks like "if I have scenario then I need its name else I have scenario outline and I need a lot of information" imho it is hard to replace such code until you have better wording for the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Chris Parsons chrismdp modified the milestone: 2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 14, 2013
  1. Matt Wynne
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 11 deletions.
  1. +33 −11 features/before_hook.feature
44 features/before_hook.feature
View
@@ -1,6 +1,6 @@
Feature: Before Hook
- Scenario: Examine names of elements
+ Background:
Given a file named "features/foo.feature" with:
"""
Feature: Feature name
@@ -15,19 +15,41 @@ Feature: Before Hook
| <placeholder> |
| step |
"""
- And a file named "features/support/hook.rb" with:
+
+ Scenario: Examine object passed into hook
+
+ The object yielded to the Before hook is a `Cucumber::Unit`
+ which represents a test-case executed by Cucumber. Each Unit
+ has a _source_, which could either be a regular Scenario, or
+ a row from a Scenario Outline's Examples table.
+
+ If you need to look at the source, you can send a visitor
+ to `Unit#describe_source_to` and the unit will call back
+ to different methods on the visitor depending on the type
+ of source the Unit originated from.
+
+ Given a file named "features/support/hook.rb" with:
"""
- names = []
- Before do |scenario|
- unless scenario.respond_to?(:scenario_outline)
- names << scenario.feature.name.split("\n").first
- names << scenario.name.split("\n").first
- else
- names << scenario.scenario_outline.feature.name.split("\n").first
- names << scenario.scenario_outline.name.split("\n").first
+ $names = []
+ at_exit { puts $names.join("\n") }
+
+ class UnitVisitor
+ def scenario(scenario)
+ $names << scenario.name
end
+
+ def scenario_outline_example(example_row)
+ $names << example_row.scenario_outline_name
+ $names << example_row.examples_table_name
+ $names << example_row.number
+ end
+ end
+
+ visitor = UnitVisitor.new
+ Before do |unit|
+ $names << unit.feature_name
+ unit.describe_source_to(visitor)
end
- at_exit { puts names.join("\n") }
"""
When I run `cucumber`
Then the output should contain:
Something went wrong with that request. Please try again.