diff --git a/features/step_definitions/debug_steps.rb b/features/step_definitions/debug_steps.rb new file mode 100644 index 0000000000000000000000000000000000000000..c56973edf9b02994630377c78e891d7645065a18 --- /dev/null +++ b/features/step_definitions/debug_steps.rb @@ -0,0 +1,4 @@ +When 'I debug' do + debugger + true +end diff --git a/features/step_definitions/factory_steps.rb b/features/step_definitions/factory_steps.rb new file mode 100644 index 0000000000000000000000000000000000000000..cf1495e999edc0636e376c5ad516cdc7885fc82d --- /dev/null +++ b/features/step_definitions/factory_steps.rb @@ -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 diff --git a/features/step_definitions/scope_steps.rb b/features/step_definitions/scope_steps.rb new file mode 100644 index 0000000000000000000000000000000000000000..cb39d33ada2d5b5f098dd7944401f67ceb4a2811 --- /dev/null +++ b/features/step_definitions/scope_steps.rb @@ -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 diff --git a/features/step_definitions/session_steps.rb b/features/step_definitions/session_steps.rb new file mode 100644 index 0000000000000000000000000000000000000000..abc9e87df0a20d5b5557f977c7d3e35db5542d07 --- /dev/null +++ b/features/step_definitions/session_steps.rb @@ -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 diff --git a/features/support/paths.rb b/features/support/paths.rb index 06b3efb0bd6b90b3f7987f2bb3c610a8cf68b498..3d3f64724f0b9e1591eed06d69de05fa58490090 100644 --- a/features/support/paths.rb +++ b/features/support/paths.rb @@ -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