diff options
author | Jamie Winsor <jamie@enmasse.com> | 2011-08-01 11:27:28 -0700 |
---|---|---|
committer | Jamie Winsor <jamie@enmasse.com> | 2011-08-03 18:01:32 -0700 |
commit | 21b19db5a30fcb6db83f4ac9302cc94c6320a0db (patch) | |
tree | 62a3b89d0f4c9b239bfd76ec77cfc3269ac041ef /features | |
parent | 3454a9c3007a4bfd10ccd0ff39dc594ebb2a0184 (diff) | |
download | refinerycms-blog-21b19db5a30fcb6db83f4ac9302cc94c6320a0db.tar.gz refinerycms-blog-21b19db5a30fcb6db83f4ac9302cc94c6320a0db.tar.bz2 refinerycms-blog-21b19db5a30fcb6db83f4ac9302cc94c6320a0db.zip |
refactor engine testing scenario
Engine is now tested standalone by leveraging a dummy rails app
Enable Guard for speedy testing
Move factories to the more standard location `spec/factories/*`
Update README with a Testing section
Rename migrations to contain datetimestamps for their version to fix migration order issues when migrating the dummy application
Diffstat (limited to 'features')
-rw-r--r-- | features/step_definitions/authors_steps.rb (renamed from features/support/step_definitions/authors_steps.rb) | 0 | ||||
-rw-r--r-- | features/step_definitions/category_steps.rb (renamed from features/support/step_definitions/category_steps.rb) | 0 | ||||
-rw-r--r-- | features/step_definitions/tags_steps.rb (renamed from features/support/step_definitions/tags_steps.rb) | 0 | ||||
-rw-r--r-- | features/step_definitions/user_steps.rb | 42 | ||||
-rw-r--r-- | features/step_definitions/web_steps.rb | 196 | ||||
-rw-r--r-- | features/support/env.rb | 50 | ||||
-rw-r--r-- | features/support/paths.rb | 34 | ||||
-rw-r--r-- | features/support/selectors.rb | 39 |
8 files changed, 341 insertions, 20 deletions
diff --git a/features/support/step_definitions/authors_steps.rb b/features/step_definitions/authors_steps.rb index eaa5f32..eaa5f32 100644 --- a/features/support/step_definitions/authors_steps.rb +++ b/features/step_definitions/authors_steps.rb diff --git a/features/support/step_definitions/category_steps.rb b/features/step_definitions/category_steps.rb index 3c197e2..3c197e2 100644 --- a/features/support/step_definitions/category_steps.rb +++ b/features/step_definitions/category_steps.rb diff --git a/features/support/step_definitions/tags_steps.rb b/features/step_definitions/tags_steps.rb index d16f758..d16f758 100644 --- a/features/support/step_definitions/tags_steps.rb +++ b/features/step_definitions/tags_steps.rb diff --git a/features/step_definitions/user_steps.rb b/features/step_definitions/user_steps.rb new file mode 100644 index 0000000..002fa7d --- /dev/null +++ b/features/step_definitions/user_steps.rb @@ -0,0 +1,42 @@ +def login(options = {}) + options = {:user => @refinery_user}.merge(options) + visit new_refinery_user_session_path + fill_in("refinery_user_login", :with => options[:user].email) + fill_in("refinery_user_password", :with => 'greenandjuicy') + click_button("submit_button") +end + +Given /^I am a logged in refinery user$/i do + @refinery_user ||= Factory(:refinery_user) + login(:user => @refinery_user) +end + +Given /^I am a logged in refinery translator$/i do + @refinery_translator ||= Factory(:refinery_translator) + login(:user => @refinery_translator) +end + +Given /^I am a logged in customer$/i do + @user ||= Factory(:user) + login(:user => @user) +end + +Given /^A Refinery user exists$/i do + @refinery_user ||= Factory(:refinery_user) +end + +Given /^I have a user named "(.*)"$/ do |name| + @user = Factory(:user, :username => name) +end + +Given /^I have a refinery user named "(.*)"$/i do |name| + @refinery_user = Factory(:refinery_user, :username => name) +end + +Given /^I have no users$/ do + ::Refinery::User.delete_all +end + +Then /^I should have ([0-9]+) users?$/ do |count| + ::Refinery::User.count.should == count.to_i +end diff --git a/features/step_definitions/web_steps.rb b/features/step_definitions/web_steps.rb new file mode 100644 index 0000000..22abbf6 --- /dev/null +++ b/features/step_definitions/web_steps.rb @@ -0,0 +1,196 @@ +# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril. +# It is recommended to regenerate this file in the future when you upgrade to a +# newer version of cucumber-rails. Consider adding your own code to a new file +# instead of editing this one. Cucumber will automatically load all features/**/*.rb +# files. + +require 'uri' +require 'cgi' +require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths")) +require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "selectors")) + +module WithinHelpers + def with_scope(locator) + locator ? within(*selector_for(locator)) { yield } : yield + end +end +World(WithinHelpers) + +# Single-line step scoper +When /^(.*) within (.*[^:])$/ do |step, parent| + with_scope(parent) { When step } +end + +# Multi-line step scoper +When /^(.*) within (.*[^:]):$/ do |step, parent, table_or_string| + with_scope(parent) { When "#{step}:", table_or_string } +end + +Given /^(?:|I )am on (.+)$/ do |page_name| + visit path_to(page_name) +end + +When /^(?:|I )go to (.+)$/ do |page_name| + visit path_to(page_name) +end + +When /^(?:|I )press "([^"]*)"$/ do |button| + click_button(button) +end + +When /^(?:|I )follow "([^"]*)"$/ do |link| + click_link(link) +end + +When /^(?:|I )fill in "([^"]*)" with "([^"]*)"$/ do |field, value| + fill_in(field, :with => value) +end + +When /^(?:|I )fill in "([^"]*)" for "([^"]*)"$/ do |value, field| + fill_in(field, :with => value) +end + +# Use this to fill in an entire form with data from a table. Example: +# +# When I fill in the following: +# | Account Number | 5002 | +# | Expiry date | 2009-11-01 | +# | Note | Nice guy | +# | Wants Email? | | +# +# TODO: Add support for checkbox, select og option +# based on naming conventions. +# +When /^(?:|I )fill in the following:$/ do |fields| + fields.rows_hash.each do |name, value| + When %{I fill in "#{name}" with "#{value}"} + end +end + +When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field| + select(value, :from => field) +end + +When /^(?:|I )check "([^"]*)"$/ do |field| + check(field) +end + +When /^(?:|I )uncheck "([^"]*)"$/ do |field| + uncheck(field) +end + +When /^(?:|I )choose "([^"]*)"$/ do |field| + choose(field) +end + +When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"$/ do |path, field| + attach_file(field, File.expand_path(path)) +end + +Then /^(?:|I )should see "([^"]*)"$/ do |text| + if page.respond_to? :should + page.should have_content(text) + else + assert page.has_content?(text) + end +end + +Then /^(?:|I )should see \/([^\/]*)\/$/ do |regexp| + regexp = Regexp.new(regexp) + + if page.respond_to? :should + page.should have_xpath('//*', :text => regexp) + else + assert page.has_xpath?('//*', :text => regexp) + end +end + +Then /^(?:|I )should not see "([^"]*)"$/ do |text| + if page.respond_to? :should + page.should have_no_content(text) + else + assert page.has_no_content?(text) + end +end + +Then /^(?:|I )should not see \/([^\/]*)\/$/ do |regexp| + regexp = Regexp.new(regexp) + + if page.respond_to? :should + page.should have_no_xpath('//*', :text => regexp) + else + assert page.has_no_xpath?('//*', :text => regexp) + end +end + +Then /^the "([^"]*)" field(?: within (.*))? should contain "([^"]*)"$/ do |field, parent, value| + with_scope(parent) do + field = find_field(field) + field_value = (field.tag_name == 'textarea') ? field.text : field.value + if field_value.respond_to? :should + field_value.should =~ /#{value}/ + else + assert_match(/#{value}/, field_value) + end + end +end + +Then /^the "([^"]*)" field(?: within (.*))? should not contain "([^"]*)"$/ do |field, parent, value| + with_scope(parent) do + field = find_field(field) + field_value = (field.tag_name == 'textarea') ? field.text : field.value + if field_value.respond_to? :should_not + field_value.should_not =~ /#{value}/ + else + assert_no_match(/#{value}/, field_value) + end + end +end + +Then /^the "([^"]*)" checkbox(?: within (.*))? should be checked$/ do |label, parent| + with_scope(parent) do + field_checked = find_field(label)['checked'] + if field_checked.respond_to? :should + field_checked.should be_true + else + assert field_checked + end + end +end + +Then /^the "([^"]*)" checkbox(?: within (.*))? should not be checked$/ do |label, parent| + with_scope(parent) do + field_checked = find_field(label)['checked'] + if field_checked.respond_to? :should + field_checked.should be_false + else + assert !field_checked + end + end +end + +Then /^(?:|I )should be on (.+)$/ do |page_name| + current_path = URI.parse(current_url).path + if current_path.respond_to? :should + current_path.should == path_to(page_name) + else + assert_equal path_to(page_name), current_path + end +end + +Then /^(?:|I )should have the following query string:$/ do |expected_pairs| + query = URI.parse(current_url).query + actual_params = query ? CGI.parse(query) : {} + expected_params = {} + expected_pairs.rows_hash.each_pair{|k,v| expected_params[k] = v.split(',')} + + if actual_params.respond_to? :should + actual_params.should == expected_params + else + assert_equal expected_params, actual_params + end +end + +Then /^show me the page$/ do + save_and_open_page +end diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..a84d601 --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,50 @@ +require 'rubygems' +require 'bundler/setup' +require 'spork' + +Spork.prefork do + require 'cucumber/rails' + require 'capybara/rails' + require 'capybara/cucumber' + require 'capybara/session' + + require 'factory_girl' + require 'database_cleaner' + require 'database_cleaner/cucumber' + + Dir[File.expand_path("../../../spec/factories/*.rb", __FILE__)].each {|f| require f} + + include ::Devise::Controllers::UrlHelpers + + # Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In + # order to ease the transition to Capybara we set the default here. If you'd + # prefer to use XPath just remove this line and adjust any selectors in your + # steps to use the XPath syntax. + Capybara.default_selector = :css + + # By default, any exception happening in your Rails application will bubble up + # to Cucumber so that your scenario will fail. This is a different from how + # your application behaves in the production environment, where an error page will + # be rendered instead. + # + # Sometimes we want to override this default behaviour and allow Rails to rescue + # exceptions and display an error page (just like when the app is running in production). + # Typical scenarios where you want to do this is when you test your error pages. + # There are two ways to allow Rails to rescue exceptions: + # + # 1) Tag your scenario (or feature) with @allow-rescue + # + # 2) Set the value below to true. Beware that doing this globally is not + # recommended as it will mask a lot of errors for you! + # + ActionController::Base.allow_rescue = false + + DatabaseCleaner.strategy = :truncation + + Before { DatabaseCleaner.start } + After { DatabaseCleaner.clean } +end + +Spork.each_run do + # This code will be run each time you run your specs. +end diff --git a/features/support/paths.rb b/features/support/paths.rb index 1fdf9f8..f69b8fe 100644 --- a/features/support/paths.rb +++ b/features/support/paths.rb @@ -1,24 +1,18 @@ -module NavigationHelpers - module Refinery - module Blog - def path_to(page_name) - case page_name - when /the list of blog posts/ - refinery_admin_blog_posts_path - when /the new blog posts? form/ - new_refinery_admin_blog_post_path - else - begin - if page_name =~ /the blog post titled "?([^\"]*)"?/ and (page = BlogPost.find_by_title($1)).present? - self.url_for(page.url) - else - nil - end - rescue - nil - end - end +def path_to(page_name) + case page_name + when /the list of blog posts/ + refinery_admin_blog_posts_path + when /the new blog posts? form/ + new_refinery_admin_blog_post_path + else + begin + if page_name =~ /the blog post titled "?([^\"]*)"?/ and (page = BlogPost.find_by_title($1)).present? + self.url_for(page.url) + else + nil end + rescue + nil end end end diff --git a/features/support/selectors.rb b/features/support/selectors.rb new file mode 100644 index 0000000..44e34a4 --- /dev/null +++ b/features/support/selectors.rb @@ -0,0 +1,39 @@ +module HtmlSelectorsHelpers + # Maps a name to a selector. Used primarily by the + # + # When /^(.+) within (.+)$/ do |step, scope| + # + # step definitions in web_steps.rb + # + def selector_for(locator) + case locator + + when "the page" + "html > body" + + # Add more mappings here. + # Here is an example that pulls values out of the Regexp: + # + # when /^the (notice|error|info) flash$/ + # ".flash.#{$1}" + + # You can also return an array to use a different selector + # type, like: + # + # when /the header/ + # [:xpath, "//header"] + + # This allows you to provide a quoted selector as the scope + # for "within" steps as was previously the default for the + # web steps: + when /^"(.+)"$/ + $1 + + else + raise "Can't find mapping from \"#{locator}\" to a selector.\n" + + "Now, go and add a mapping in #{__FILE__}" + end + end +end + +World(HtmlSelectorsHelpers) |