Add the Hashrocket default step definitions

Tweaked for mongomapper and diaspora
This commit is contained in:
Stephen Caudill 2010-09-25 15:20:08 -04:00
parent 05c2da81e5
commit 8a93c67166
5 changed files with 135 additions and 24 deletions

View file

@ -0,0 +1,4 @@
When 'I debug' do
debugger
true
end

View file

@ -0,0 +1,68 @@
module FactoryMethods
def create_from_table(model_name, table, extra = {})
factory_name = model_name.gsub(/\W+/, '_').downcase.singularize.to_sym
is_singular = model_name.to_s.singularize == model_name.to_s
hashes = if is_singular
if table.kind_of?(Hash)
[table]
else
[table.rows_hash]
end
else
table.hashes
end
klass = Factory.factories[factory_name].class_name.to_s.classify.constantize
@they = hashes.map do |hash|
hash = hash.merge(extra).inject({}) do |h,(k,v)|
k = k.gsub(/\W+/,'_')
v = v.split(/\s*,\s*/) if klass.serialized_attributes[k] == Array
h.update(k.to_sym => v)
end
object = Factory.build(factory_name, hash)
yield object if block_given?
object.save!
object
end
if is_singular
@it = @they.last
instance_variable_set("@#{factory_name}", @it)
end
end
end
World(FactoryMethods)
Given %r{^I have a (.+)$} do |model_name|
create_from_table(model_name, {}, 'user' => @me)
end
Given %r{^I have the following (.+):$} do |child, table|
Given "that me has the following #{child}:", table
end
Given %r{^the following (.+):$} do |model_name, table|
create_from_table(model_name, table)
end
Given %r{^that (.+) has the following (.+):$} do |parent, child, table|
child= child.gsub(/\W+/,'_')
parent = parent.gsub(/\W+/,'_').downcase.sub(/^_/, '')
parent_instance = instance_variable_get("@#{parent}")
parent_class = parent_instance.class
if assoc = parent_class.reflect_on_association(child.to_sym) || parent_class.reflect_on_association(child.pluralize.to_sym)
parent = (assoc.options[:as] || parent).to_s
child = (assoc.options[:class_name] || child).to_s
end
if child.classify.constantize.method_defined?(parent.pluralize)
create_from_table(child, table, parent.pluralize => [parent_instance])
elsif child.classify.constantize.method_defined?(parent)
create_from_table(child, table, parent => parent_instance)
else
create_from_table(child, table)
if assoc.macro == :has_many
parent_instance.send("#{assoc.name}=", @they)
else
parent_instance.send("#{assoc.name}=", @they.first)
end
end
end

View file

@ -0,0 +1,30 @@
module SectionLocator
def within_parent(content, elements = ['*'], &block)
expr = %(//*[(#{elements.join('|')})/descendant-or-self::*[contains(., "#{content}")]])
within(expr, &block)
end
end
World(SectionLocator)
sections = %w(h1 h2 h3 h4 h5 h6 legend caption dt strong)
When /^(.*) in the "([^\"]*)" section$/ do |action, title|
within_parent(title, sections) do
When action
end
end
When /^(.*) in the "([^\"]*)" section:$/ do |action, title, table|
within_parent(title, sections) do
When "#{action}:", table
end
end
When /^(.*) in the "([^\"]*)" row$/ do |action, title|
within_parent(title, %w(th td)) do
When action
end
end

View file

@ -0,0 +1,26 @@
Given /^I am signed in as the following (\w+):$/ do |role, table|
Given %(the following #{role}:), table
@me = @it
Given 'I am signed in'
end
Given /^I (?:am signed|sign) in as an? (\w+)$/ do |role|
@me = Factory(role.to_sym)
Given 'I am signed in'
end
Given 'I am signed in' do
@me ||= Factory(:user)
When %(I go to the new user session page)
When %(I fill in "Username" with "#{@me.username}")
When %(I fill in "Password" with "#{@me.password}")
When %(I press "Sign in")
end
When /^I sign in as "([^"]*)"$/ do |email|
@me = User.find_by_email(email)
@me.password ||= 'password'
Given 'I am signed in'
end

View file

@ -1,31 +1,14 @@
module NavigationHelpers
# Maps a name to a path. Used by the
#
# When /^I go to (.+)$/ do |page_name|
#
# step definition in web_steps.rb
#
def path_to(page_name)
case page_name
when /the home\s?page/
'/'
# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
#
# when /^(.*)'s profile page$/i
# user_profile_path(User.find_by_login($1))
when /^its ([\w ]+) page$/
send("#{$1.gsub(/\W+/, '_')}_path", @it)
when /^the ([\w ]+) page$/
send("#{$1.gsub(/\W+/, '_')}_path")
when /^"(\/.*)"/
$1
else
begin
page_name =~ /the (.*) page/
path_components = $1.split(/\s+/)
self.send(path_components.push('path').join('_').to_sym)
rescue Object => e
raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
"Now, go and add a mapping in #{__FILE__}"
end
raise "Can't find mapping from \"#{page_name}\" to a path."
end
end
end