Skip to content

Instantly share code, notes, and snippets.

@burtlo
Created November 22, 2010 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save burtlo/710405 to your computer and use it in GitHub Desktop.
Save burtlo/710405 to your computer and use it in GitHub Desktop.
Nested Step Definitions
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 ...
#
@burtlo
Copy link
Author

burtlo commented Dec 13, 2010

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),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment