public
Last active

Test Data Builder Cucumber Steps

  • Download Gist
step_definitions/builder_steps.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
module BuilderStepHelpers
 
def create_model(model_name, attributes={})
send("create_#{model_name.gsub(' ','_')}",attributes)
end
 
end
World do |world|
world.extend BuilderStepHelpers
end
 
# Examples:
# Given the following widget exists:
# | Name | Price |
# | Foo | 20.00 |
# Given the following pets exist:
# | Pet Type | Months Old |
# | Dog | 23 |
# | Cat | 34 |
Given /^the following (.+?)(?:s|) exist(?:s|):$/ do |model_name, table|
table.hashes.each do |hash|
attributes = {}
hash.each { |k, v| attributes[k.gsub(' ','').underscore] = v }
create_model(model_name, attributes)
end
end
 
# Example:
# Given widgets named 'Foo', 'Bar', and 'Car' exist
Given /^(.+?)(?:s|) named (.+) exist$/ do |model_name, names|
names.extract_list.each do |name|
create_model(model_name, {:name => name})
end
end
 
# Example:
# Given an expensive widget exists (assumes you have a create_expensive_widget method)
# Given a widget exists
# Given 3 widgets exist
# Given 33 widgets exist
#
# Warning: this one can be a little too greedy at times so YMMV from project to project.
Given /^(a|an|\d+) (.+?)(?:s|) exist(?:s|)$/ do |ammount, model_name|
how_many = ammount =~ /a|an/ ? 1 : ammount.to_i
1.upto(how_many) { create_model(model_name) }
end
support/string.rb
Ruby
1 2 3 4 5 6 7 8
# Examples:
# "'Foo', 'Bar', and 'Jar'".extract_list # => ["Foo", "Bar", "Jar"]
# '"Dog", "Cat"'.extract_list # => ["Dog", "Cat"]
class String
def extract_list
self.scan((/['"](.*?)["']/)).flatten
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.