Created
November 22, 2010 18:40
-
-
Save burtlo/710405 to your computer and use it in GitHub Desktop.
Nested Step Definitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module EventsHelper | |
def create_events(events_table) | |
events_table.map_headers! {|header| header.is_a?(Symbol) ? header : header.gsub(/\s/,'_').to_sym } | |
system_time = TimeService.now | |
events_table.hashes.each do |event| | |
event[:name] ||= "Event #{system_time.strftime("%H:%M:%S")}" | |
event[:enabled] ||= true | |
event[:time] ||= system_time | |
Event.queue(data) | |
end | |
Event.create | |
end | |
end | |
World(EventsHelper) | |
# | |
# Create the following type of event statement | |
# with the data specified in the table | |
# | |
Given /^(?:I create )?the following events:$/ do |events| | |
create_events(events) | |
end | |
# | |
# Recently Logged in event | |
# | |
Given /^(?:I create )?an event, named '([^']*)', that a customer has logged in within the last (\d+) minutes?$/ do |name,recency| | |
data = [ [ 'name', 'type', 'how recent (in minutes)' ], [ name, 'Recent Communication' recency ]] | |
create_events Cucumber::Ast::Table.new(data) | |
end | |
# | |
# Purchase event | |
# | |
Given /^(?:I create )?an event, named '([^']*)', that a customer has purchased '([^']*)' within the last (\d+)\s?(?:-|to)?\s?(\d+) days?$/ do |name,product_name,start_day,end_day| | |
data = [ [ 'name', 'type', 'product name', 'start day', 'end day' ], [ name, 'Product Purchase', product_name, start_day, end_day ]] | |
create_events Cucumber::Ast::Table.new(data) | |
end | |
# | |
# ... more events ... | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From Mattwyne's fork. This is great. Thanks for taking the refactor further.
I think this would be a great example to share on the Cucumber Wiki.
Multiple similar steps
Refactor steps to the collector step
Refactor out the helper
I think that if we showed the first version with the redundancy within the step definitions, then my step at refactoring, then your final refactoring step (a beautiful bow),