Skip to content

Instantly share code, notes, and snippets.

@adamzaninovich
Created December 4, 2010 19:33
Show Gist options
  • Save adamzaninovich/728413 to your computer and use it in GitHub Desktop.
Save adamzaninovich/728413 to your computer and use it in GitHub Desktop.
I just started using cucumber. So far, I've tried to write my steps so that they are very reusable. These were written for mongoid, but they should work with ActiveRecord as well.
# I only have recipes named Steak, Lobster
# I have a recipe named Pizza
Given /^I(\sonly)? have a?n?\s?(.+) named (.+)$/ do |only,table,names|
table.singularize.capitalize.constantize.destroy_all if only
names.split(', ').each do |name|
table.singularize.capitalize.constantize.create(:name => name)
end
end
# I have no recipes
Given /^I have no (.+)$/ do |table|
table.singularize.capitalize.constantize.destroy_all
end
# the recipe named Steak has no ingredients
Given /^the (.+) named (.+) has no (.+)$/ do |table,name,subtable|
table.singularize.capitalize.constantize.
where(:name => name).first.
send(subtable.pluralize).destroy_all
end
# the recipe named Steak has 3 blank steps
Given /^the (.+) named (.+) has (\d+) blank (.+)$/ do |table,name,number,subtable|
number.to_i.times do
table.singularize.capitalize.constantize.
where(:name => name).first.
send(subtable.pluralize).create!()
end
end
# I should have no recipes
# I should have 1 recipe
Then /^I should have (\d+|no) (.+)$/ do |number,table|
table.singularize.capitalize.constantize.
count.should == (number=='no' ? 0 : number.to_i)
end
# the recipe named Steak should have 3 steps
# the recipe named Steak should have no ingredients
Then /^the (.+) named (.+) should have (\d+|no) (.+)$/ do |table,name,number,subtable|
table.singularize.capitalize.constantize.
where(:name => name).first.
send(subtable.pluralize).
count.should == (number=='no' ? 0 : number.to_i)
end
@adamzaninovich
Copy link
Author

If anyone knows a Rails helper that does singularize.capitalize.constantize in one method, let me know. I wouldn't know what to call it.

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