From fb5809ae1b0d24fa159b7325a68d5be76c9ecca9 Mon Sep 17 00:00:00 2001 From: Matthew Landauer Date: Thu, 27 Jan 2011 17:03:33 +1100 Subject: [PATCH] Upgrade rspec and cucumber to rails 3 compatible version --- .rspec | 1 + Gemfile | 21 ++-- config/cucumber.yml | 7 +- features/step_definitions/email_steps.rb | 91 ++++++++++---- features/step_definitions/web_steps.rb | 116 ++++++++++-------- features/support/env.rb | 16 ++- features/support/paths.rb | 12 +- lib/tasks/cucumber.rake | 8 +- lib/tasks/rspec.rake | 144 ----------------------- spec/models/alert_notifier_spec.rb | 2 +- spec/models/comment_notifier_spec.rb | 2 +- spec/models/notifier_spec.rb | 10 +- spec/spec_helper.rb | 61 +++------- spec/views/applications/show_spec.rb | 51 ++++---- spec/views/confirm/alert_spec.rb | 24 ++-- spec/views/static/about_spec.rb | 10 +- 16 files changed, 247 insertions(+), 329 deletions(-) create mode 100644 .rspec delete mode 100644 lib/tasks/rspec.rake diff --git a/.rspec b/.rspec new file mode 100644 index 000000000..53607ea52 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--colour diff --git a/Gemfile b/Gemfile index e3e4d5e1d..72e1e66ad 100644 --- a/Gemfile +++ b/Gemfile @@ -28,16 +28,15 @@ gem "compass", ">= 0.10.6" gem 'fancy-buttons' gem "rails-geocoder", :require => "geocoder" -group :cucumber do - gem 'cucumber-rails', '>=0.2.4' - gem 'database_cleaner', '>=0.4.3' - gem 'webrat', '>=0.6.0' +group :test do + gem 'cucumber-rails' + #gem 'capybara' + gem 'webrat' + gem 'database_cleaner' + gem 'factory_girl_rails' + gem 'email_spec' end - -group :cucumber, :test do - gem 'rspec-rails', '~> 1.3.2' - # This is the latest version of the gem that is compatible with Rails 2 - gem 'factory_girl', '1.2.4' - # Version 1.0.0 is for Rails 3 so can't use that yet - gem 'email_spec', '< 1.0.0', :require => 'email_spec' + +group :test, :development do + gem 'rspec-rails', '~> 2.4' end diff --git a/config/cucumber.yml b/config/cucumber.yml index b7acd380e..621a14cea 100644 --- a/config/cucumber.yml +++ b/config/cucumber.yml @@ -1,7 +1,8 @@ <% rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : "" -rerun_opts = rerun.to_s.strip.empty? ? "--format progress features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}" -std_opts = "#{rerun_opts} --format rerun --out rerun.txt --strict --tags ~@wip" +rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}" +std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} --strict --tags ~@wip" %> -default: <%= std_opts %> +default: <%= std_opts %> features wip: --tags @wip:3 --wip features +rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip diff --git a/features/step_definitions/email_steps.rb b/features/step_definitions/email_steps.rb index d5d3276a1..65fca46da 100644 --- a/features/step_definitions/email_steps.rb +++ b/features/step_definitions/email_steps.rb @@ -56,13 +56,12 @@ def current_email_address mailbox_for(address).size.should == parse_email_count(amount) end -# DEPRECATED -# The following methods are left in for backwards compatibility and -# should be removed by version 0.4.0 -Then /^(?:I|they|"([^"]*?)") should not receive an email$/ do |address| - email_spec_deprecate "The step 'I/they/[email] should not receive an email' is no longer supported. - Please use 'I/they/[email] should receive no emails' instead." - unread_emails_for(address).size.should == 0 +Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject "([^"]*?)"$/ do |address, amount, subject| + unread_emails_for(address).select { |m| m.subject =~ Regexp.new(subject) }.size.should == parse_email_count(amount) +end + +Then /^(?:I|they|"([^"]*?)") should receive an email with the following body:$/ do |address, expected_body| + open_email(address, :with_text => expected_body) end # @@ -95,11 +94,11 @@ def current_email_address end Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text| - current_email.body.should include(text) + current_email.default_part_body.to_s.should include(text) end Then /^(?:I|they) should see \/([^"]*?)\/ in the email body$/ do |text| - current_email.body.should =~ Regexp.new(text) + current_email.default_part_body.to_s.should =~ Regexp.new(text) end Then /^(?:I|they) should see the email delivered from "([^"]*?)"$/ do |text| @@ -114,19 +113,50 @@ def current_email_address current_email.should have_header(name, Regexp.new(text)) end +Then /^I should see it is a multi\-part email$/ do + current_email.should be_multipart +end -# DEPRECATED -# The following methods are left in for backwards compatibility and -# should be removed by version 0.4.0. -Then /^(?:I|they) should see "([^"]*?)" in the subject$/ do |text| - email_spec_deprecate "The step 'I/they should see [text] in the subject' is no longer supported. - Please use 'I/they should see [text] in the email subject' instead." - current_email.should have_subject(Regexp.new(text)) +Then /^(?:I|they) should see "([^"]*?)" in the email html part body$/ do |text| + current_email.html_part.body.to_s.should include(text) end -Then /^(?:I|they) should see "([^"]*?)" in the email$/ do |text| - email_spec_deprecate "The step 'I/they should see [text] in the email' is no longer supported. - Please use 'I/they should see [text] in the email body' instead." - current_email.body.should =~ Regexp.new(text) + +Then /^(?:I|they) should see "([^"]*?)" in the email text part body$/ do |text| + current_email.text_part.body.to_s.should include(text) +end + +# +# Inspect the Email Attachments +# + +Then /^(?:I|they) should see (an|no|\d+) attachments? with the email$/ do |amount| + current_email_attachments.size.should == parse_email_count(amount) +end + +Then /^there should be (an|no|\d+) attachments? named "([^"]*?)"$/ do |amount, filename| + current_email_attachments.select { |a| a.filename == filename }.size.should == parse_email_count(amount) +end + +Then /^attachment (\d+) should be named "([^"]*?)"$/ do |index, filename| + current_email_attachments[(index.to_i - 1)].filename.should == filename +end + +Then /^there should be (an|no|\d+) attachments? of type "([^"]*?)"$/ do |amount, content_type| + current_email_attachments.select { |a| a.content_type.include?(content_type) }.size.should == parse_email_count(amount) +end + +Then /^attachment (\d+) should be of type "([^"]*?)"$/ do |index, content_type| + current_email_attachments[(index.to_i - 1)].content_type.should include(content_type) +end + +Then /^all attachments should not be blank$/ do + current_email_attachments.each do |attachment| + attachment.read.size.should_not == 0 + end +end + +Then /^show me a list of email attachments$/ do + EmailSpec::EmailViewer::save_and_open_email_attachments_list(current_email) end # @@ -141,3 +171,24 @@ def current_email_address click_first_link_in_email end +# +# Debugging +# These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command. +# Patches accepted. ;) +# + +Then /^save and open current email$/ do + EmailSpec::EmailViewer::save_and_open_email(current_email) +end + +Then /^save and open all text emails$/ do + EmailSpec::EmailViewer::save_and_open_all_text_emails +end + +Then /^save and open all html emails$/ do + EmailSpec::EmailViewer::save_and_open_all_html_emails +end + +Then /^save and open all raw emails$/ do + EmailSpec::EmailViewer::save_and_open_all_raw_emails +end diff --git a/features/step_definitions/web_steps.rb b/features/step_definitions/web_steps.rb index ad0a26a39..b75709610 100644 --- a/features/step_definitions/web_steps.rb +++ b/features/step_definitions/web_steps.rb @@ -6,6 +6,7 @@ require 'uri' +require 'cgi' require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths")) # Commonly used webrat steps @@ -19,23 +20,23 @@ visit path_to(page_name) end -When /^(?:|I )press "([^\"]*)"$/ do |button| +When /^(?:|I )press "([^"]*)"$/ do |button| click_button(button) end -When /^(?:|I )follow "([^\"]*)"$/ do |link| +When /^(?:|I )follow "([^"]*)"$/ do |link| click_link(link) end -When /^(?:|I )follow "([^\"]*)" within "([^\"]*)"$/ do |link, parent| +When /^(?:|I )follow "([^"]*)" within "([^"]*)"$/ do |link, parent| click_link_within(parent, link) end -When /^(?:|I )fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value| +When /^(?:|I )fill in "([^"]*)" with "([^"]*)"$/ do |field, value| fill_in(field, :with => value) end -When /^(?:|I )fill in "([^\"]*)" for "([^\"]*)"$/ do |value, field| +When /^(?:|I )fill in "([^"]*)" for "([^"]*)"$/ do |value, field| fill_in(field, :with => value) end @@ -56,13 +57,13 @@ end end -When /^(?:|I )select "([^\"]*)" from "([^\"]*)"$/ do |value, field| +When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field| select(value, :from => field) end # Use this step in conjunction with Rail's datetime_select helper. For example: # When I select "December 25, 2008 10:00" as the date and time -When /^(?:|I )select "([^\"]*)" as the date and time$/ do |time| +When /^(?:|I )select "([^"]*)" as the date and time$/ do |time| select_datetime(time) end @@ -75,7 +76,7 @@ # The following steps would fill out the form: # When I select "November 23, 2004 11:20" as the "Preferred" date and time # And I select "November 25, 2004 10:30" as the "Alternative" date and time -When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date and time$/ do |datetime, datetime_label| +When /^(?:|I )select "([^"]*)" as the "([^"]*)" date and time$/ do |datetime, datetime_label| select_datetime(datetime, :from => datetime_label) end @@ -83,46 +84,46 @@ # When I select "2:20PM" as the time # Note: Rail's default time helper provides 24-hour time-- not 12 hour time. Webrat # will convert the 2:20PM to 14:20 and then select it. -When /^(?:|I )select "([^\"]*)" as the time$/ do |time| +When /^(?:|I )select "([^"]*)" as the time$/ do |time| select_time(time) end # Use this step when using multiple time_select helpers on a page or you want to # specify the name of the time on the form. For example: # When I select "7:30AM" as the "Gym" time -When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" time$/ do |time, time_label| +When /^(?:|I )select "([^"]*)" as the "([^"]*)" time$/ do |time, time_label| select_time(time, :from => time_label) end # Use this step in conjunction with Rail's date_select helper. For example: # When I select "February 20, 1981" as the date -When /^(?:|I )select "([^\"]*)" as the date$/ do |date| +When /^(?:|I )select "([^"]*)" as the date$/ do |date| select_date(date) end # Use this step when using multiple date_select helpers on one page or # you want to specify the name of the date on the form. For example: # When I select "April 26, 1982" as the "Date of Birth" date -When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date$/ do |date, date_label| +When /^(?:|I )select "([^"]*)" as the "([^"]*)" date$/ do |date, date_label| select_date(date, :from => date_label) end -When /^(?:|I )check "([^\"]*)"$/ do |field| +When /^(?:|I )check "([^"]*)"$/ do |field| check(field) end -When /^(?:|I )uncheck "([^\"]*)"$/ do |field| +When /^(?:|I )uncheck "([^"]*)"$/ do |field| uncheck(field) end -When /^(?:|I )choose "([^\"]*)"$/ do |field| +When /^(?:|I )choose "([^"]*)"$/ do |field| choose(field) end # Adds support for validates_attachment_content_type. Without the mime-type getting # passed to attach_file() you will get a "Photo file is not one of the allowed file types." # error message -When /^(?:|I )attach the file "([^\"]*)" to "([^\"]*)"$/ do |path, field| +When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"$/ do |path, field| type = path.split(".")[1] case type @@ -139,17 +140,17 @@ attach_file(field, path, type) end -Then /^(?:|I )should see "([^\"]*)"$/ do |text| - if defined?(Spec::Rails::Matchers) +Then /^(?:|I )should see "([^"]*)"$/ do |text| + if response.respond_to? :should response.should contain(text) else assert_contain text end end -Then /^(?:|I )should see "([^\"]*)" within "([^\"]*)"$/ do |text, selector| +Then /^(?:|I )should see "([^"]*)" within "([^"]*)"$/ do |text, selector| within(selector) do |content| - if defined?(Spec::Rails::Matchers) + if content.respond_to? :should content.should contain(text) else hc = Webrat::Matchers::HasContent.new(text) @@ -160,17 +161,17 @@ Then /^(?:|I )should see \/([^\/]*)\/$/ do |regexp| regexp = Regexp.new(regexp) - if defined?(Spec::Rails::Matchers) + if response.respond_to? :should response.should contain(regexp) else assert_match(regexp, response_body) end end -Then /^(?:|I )should see \/([^\/]*)\/ within "([^\"]*)"$/ do |regexp, selector| +Then /^(?:|I )should see \/([^\/]*)\/ within "([^"]*)"$/ do |regexp, selector| within(selector) do |content| regexp = Regexp.new(regexp) - if defined?(Spec::Rails::Matchers) + if content.respond_to? :should content.should contain(regexp) else assert_match(regexp, content) @@ -178,17 +179,17 @@ end end -Then /^(?:|I )should not see "([^\"]*)"$/ do |text| - if defined?(Spec::Rails::Matchers) +Then /^(?:|I )should not see "([^"]*)"$/ do |text| + if response.respond_to? :should_not response.should_not contain(text) else assert_not_contain(text) end end -Then /^(?:|I )should not see "([^\"]*)" within "([^\"]*)"$/ do |text, selector| +Then /^(?:|I )should not see "([^"]*)" within "([^"]*)"$/ do |text, selector| within(selector) do |content| - if defined?(Spec::Rails::Matchers) + if content.respond_to? :should_not content.should_not contain(text) else hc = Webrat::Matchers::HasContent.new(text) @@ -199,17 +200,17 @@ Then /^(?:|I )should not see \/([^\/]*)\/$/ do |regexp| regexp = Regexp.new(regexp) - if defined?(Spec::Rails::Matchers) + if response.respond_to? :should_not response.should_not contain(regexp) else assert_not_contain(regexp) end end -Then /^(?:|I )should not see \/([^\/]*)\/ within "([^\"]*)"$/ do |regexp, selector| +Then /^(?:|I )should not see \/([^\/]*)\/ within "([^"]*)"$/ do |regexp, selector| within(selector) do |content| regexp = Regexp.new(regexp) - if defined?(Spec::Rails::Matchers) + if content.respond_to? :should_not content.should_not contain(regexp) else assert_no_match(regexp, content) @@ -217,47 +218,64 @@ end end -Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value| - if defined?(Spec::Rails::Matchers) - field_labeled(field).value.should =~ /#{value}/ +Then /^the "([^"]*)" field should contain "([^"]*)"$/ do |field, value| + field_value = field_labeled(field).value + if field_value.respond_to? :should + field_value.should =~ /#{value}/ else - assert_match(/#{value}/, field_labeled(field).value) + assert_match(/#{value}/, field_value) end end -Then /^the "([^\"]*)" field should not contain "([^\"]*)"$/ do |field, value| - if defined?(Spec::Rails::Matchers) - field_labeled(field).value.should_not =~ /#{value}/ +Then /^the "([^"]*)" field should not contain "([^"]*)"$/ do |field, value| + field_value = field_labeled(field).value + if field_value.respond_to? :should_not + field_value.should_not =~ /#{value}/ else - assert_no_match(/#{value}/, field_labeled(field).value) + assert_no_match(/#{value}/, field_value) end end -Then /^the "([^\"]*)" checkbox should be checked$/ do |label| - if defined?(Spec::Rails::Matchers) - field_labeled(label).should be_checked +Then /^the "([^"]*)" checkbox should be checked$/ do |label| + field = field_labeled(label) + if field.respond_to? :should + field.should be_checked else - assert field_labeled(label).checked? + assert field.checked? end end -Then /^the "([^\"]*)" checkbox should not be checked$/ do |label| - if defined?(Spec::Rails::Matchers) - field_labeled(label).should_not be_checked +Then /^the "([^"]*)" checkbox should not be checked$/ do |label| + field = field_labeled(label) + if field.respond_to? :should_not + field.should_not be_checked else - assert !field_labeled(label).checked? + assert !field.checked? end end Then /^(?:|I )should be on (.+)$/ do |page_name| - current_path = URI.parse(current_url).select(:path, :query).compact.join('?') - if defined?(Spec::Rails::Matchers) + 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 \ No newline at end of file +end diff --git a/features/support/env.rb b/features/support/env.rb index 41f15cf6c..4adfb0f79 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -4,7 +4,7 @@ # instead of editing this one. Cucumber will automatically load all features/**/*.rb # files. -ENV["RAILS_ENV"] ||= "cucumber" +ENV["RAILS_ENV"] ||= "test" require File.expand_path(File.dirname(__FILE__) + '/../../config/environment') require 'cucumber/formatter/unicode' # Remove this line if you don't want Cucumber Unicode support @@ -17,7 +17,7 @@ require 'webrat/core/matchers' Webrat.configure do |config| - config.mode = :rails + config.mode = :rack config.open_error_files = false # Set to true if you want error pages to pop up in the browser end @@ -46,10 +46,16 @@ # subsequent scenarios. If you do this, we recommend you create a Before # block that will explicitly put your database in a known state. Cucumber::Rails::World.use_transactional_fixtures = true - # How to clean your database when transactions are turned off. See # http://github.com/bmabey/database_cleaner for more info. -require 'database_cleaner' -DatabaseCleaner.strategy = :truncation +if defined?(ActiveRecord::Base) + begin + require 'database_cleaner' + DatabaseCleaner.strategy = :truncation + rescue LoadError => ignore_if_database_cleaner_not_present + end +end +# Make sure this require is after you require cucumber/rails/world. +#require 'email_spec' # add this line if you use spork require 'email_spec/cucumber' diff --git a/features/support/paths.rb b/features/support/paths.rb index 0b559c996..808bf0943 100644 --- a/features/support/paths.rb +++ b/features/support/paths.rb @@ -7,7 +7,7 @@ module NavigationHelpers # def path_to(page_name) case page_name - + when /the home\s?page/ '/' when /signup/ @@ -25,8 +25,14 @@ def path_to(page_name) # user_profile_path(User.find_by_login($1)) else - raise "Can't find mapping from \"#{page_name}\" to a path.\n" + - "Now, go and add a mapping in #{__FILE__}" + 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 end end end diff --git a/lib/tasks/cucumber.rake b/lib/tasks/cucumber.rake index ab04dc887..7db1a5570 100644 --- a/lib/tasks/cucumber.rake +++ b/lib/tasks/cucumber.rake @@ -7,7 +7,7 @@ unless ARGV.any? {|a| a =~ /^gems/} # Don't load anything when running the gems:* tasks -vendored_cucumber_bin = Dir["#{RAILS_ROOT}/vendor/{gems,plugins}/cucumber*/bin/cucumber"].first +vendored_cucumber_bin = Dir["#{Rails.root}/vendor/{gems,plugins}/cucumber*/bin/cucumber"].first $LOAD_PATH.unshift(File.dirname(vendored_cucumber_bin) + '/../lib') unless vendored_cucumber_bin.nil? begin @@ -26,6 +26,12 @@ begin t.profile = 'wip' end + Cucumber::Rake::Task.new({:rerun => 'db:test:prepare'}, 'Record failing features and run only them if any exist') do |t| + t.binary = vendored_cucumber_bin + t.fork = true # You may get faster startup if you set this to false + t.profile = 'rerun' + end + desc 'Run all features' task :all => [:ok, :wip] end diff --git a/lib/tasks/rspec.rake b/lib/tasks/rspec.rake deleted file mode 100644 index dba3ffcc1..000000000 --- a/lib/tasks/rspec.rake +++ /dev/null @@ -1,144 +0,0 @@ -gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 -rspec_gem_dir = nil -Dir["#{RAILS_ROOT}/vendor/gems/*"].each do |subdir| - rspec_gem_dir = subdir if subdir.gsub("#{RAILS_ROOT}/vendor/gems/","") =~ /^(\w+-)?rspec-(\d+)/ && File.exist?("#{subdir}/lib/spec/rake/spectask.rb") -end -rspec_plugin_dir = File.expand_path(File.dirname(__FILE__) + '/../../vendor/plugins/rspec') - -if rspec_gem_dir && (test ?d, rspec_plugin_dir) - raise "\n#{'*'*50}\nYou have rspec installed in both vendor/gems and vendor/plugins\nPlease pick one and dispose of the other.\n#{'*'*50}\n\n" -end - -if rspec_gem_dir - $LOAD_PATH.unshift("#{rspec_gem_dir}/lib") -elsif File.exist?(rspec_plugin_dir) - $LOAD_PATH.unshift("#{rspec_plugin_dir}/lib") -end - -# Don't load rspec if running "rake gems:*" -unless ARGV.any? {|a| a =~ /^gems/} - -begin - require 'spec/rake/spectask' -rescue MissingSourceFile - module Spec - module Rake - class SpecTask - def initialize(name) - task name do - # if rspec-rails is a configured gem, this will output helpful material and exit ... - require File.expand_path(File.join(File.dirname(__FILE__),"..","..","config","environment")) - - # ... otherwise, do this: - raise <<-MSG - -#{"*" * 80} -* You are trying to run an rspec rake task defined in -* #{__FILE__}, -* but rspec can not be found in vendor/gems, vendor/plugins or system gems. -#{"*" * 80} -MSG - end - end - end - end - end -end - -Rake.application.instance_variable_get('@tasks').delete('default') - -spec_prereq = File.exist?(File.join(RAILS_ROOT, 'config', 'database.yml')) ? "db:test:prepare" : :noop -task :noop do -end - -task :default => :spec -task :stats => "spec:statsetup" - -desc "Run all specs in spec directory (excluding plugin specs)" -Spec::Rake::SpecTask.new(:spec => spec_prereq) do |t| - t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] - t.spec_files = FileList['spec/**/*_spec.rb'] -end - -namespace :spec do - desc "Run all specs in spec directory with RCov (excluding plugin specs)" - Spec::Rake::SpecTask.new(:rcov) do |t| - t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] - t.spec_files = FileList['spec/**/*_spec.rb'] - t.rcov = true - t.rcov_opts = lambda do - IO.readlines("#{RAILS_ROOT}/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten - end - end - - desc "Print Specdoc for all specs (excluding plugin specs)" - Spec::Rake::SpecTask.new(:doc) do |t| - t.spec_opts = ["--format", "specdoc", "--dry-run"] - t.spec_files = FileList['spec/**/*_spec.rb'] - end - - desc "Print Specdoc for all plugin examples" - Spec::Rake::SpecTask.new(:plugin_doc) do |t| - t.spec_opts = ["--format", "specdoc", "--dry-run"] - t.spec_files = FileList['vendor/plugins/**/spec/**/*_spec.rb'].exclude('vendor/plugins/rspec/*') - end - - [:models, :controllers, :views, :helpers, :lib, :integration].each do |sub| - desc "Run the code examples in spec/#{sub}" - Spec::Rake::SpecTask.new(sub => spec_prereq) do |t| - t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] - t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"] - end - end - - desc "Run the code examples in vendor/plugins (except RSpec's own)" - Spec::Rake::SpecTask.new(:plugins => spec_prereq) do |t| - t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] - t.spec_files = FileList['vendor/plugins/**/spec/**/*_spec.rb'].exclude('vendor/plugins/rspec/*').exclude("vendor/plugins/rspec-rails/*") - end - - namespace :plugins do - desc "Runs the examples for rspec_on_rails" - Spec::Rake::SpecTask.new(:rspec_on_rails) do |t| - t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] - t.spec_files = FileList['vendor/plugins/rspec-rails/spec/**/*_spec.rb'] - end - end - - # Setup specs for stats - task :statsetup do - require 'code_statistics' - ::STATS_DIRECTORIES << %w(Model\ specs spec/models) if File.exist?('spec/models') - ::STATS_DIRECTORIES << %w(View\ specs spec/views) if File.exist?('spec/views') - ::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers) if File.exist?('spec/controllers') - ::STATS_DIRECTORIES << %w(Helper\ specs spec/helpers) if File.exist?('spec/helpers') - ::STATS_DIRECTORIES << %w(Library\ specs spec/lib) if File.exist?('spec/lib') - ::STATS_DIRECTORIES << %w(Routing\ specs spec/routing) if File.exist?('spec/routing') - ::STATS_DIRECTORIES << %w(Integration\ specs spec/integration) if File.exist?('spec/integration') - ::CodeStatistics::TEST_TYPES << "Model specs" if File.exist?('spec/models') - ::CodeStatistics::TEST_TYPES << "View specs" if File.exist?('spec/views') - ::CodeStatistics::TEST_TYPES << "Controller specs" if File.exist?('spec/controllers') - ::CodeStatistics::TEST_TYPES << "Helper specs" if File.exist?('spec/helpers') - ::CodeStatistics::TEST_TYPES << "Library specs" if File.exist?('spec/lib') - ::CodeStatistics::TEST_TYPES << "Routing specs" if File.exist?('spec/routing') - ::CodeStatistics::TEST_TYPES << "Integration specs" if File.exist?('spec/integration') - end - - namespace :db do - namespace :fixtures do - desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z." - task :load => :environment do - ActiveRecord::Base.establish_connection(Rails.env) - base_dir = File.join(Rails.root, 'spec', 'fixtures') - fixtures_dir = ENV['FIXTURES_DIR'] ? File.join(base_dir, ENV['FIXTURES_DIR']) : base_dir - - require 'active_record/fixtures' - (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/).map {|f| File.join(fixtures_dir, f) } : Dir.glob(File.join(fixtures_dir, '*.{yml,csv}'))).each do |fixture_file| - Fixtures.create_fixtures(File.dirname(fixture_file), File.basename(fixture_file, '.*')) - end - end - end - end -end - -end diff --git a/spec/models/alert_notifier_spec.rb b/spec/models/alert_notifier_spec.rb index 980e05e55..eea49ee67 100644 --- a/spec/models/alert_notifier_spec.rb +++ b/spec/models/alert_notifier_spec.rb @@ -19,7 +19,7 @@ it "should be from the main planningalerts email address" do @email.from.should == ["contact@planningalerts.org.au"] - @email.from_addrs.first.name.should == "PlanningAlerts.org.au" + #@email.from_addrs.first.name.should == "PlanningAlerts.org.au" end it "should have a sensible subject line" do diff --git a/spec/models/comment_notifier_spec.rb b/spec/models/comment_notifier_spec.rb index f90694b15..67cb324ea 100644 --- a/spec/models/comment_notifier_spec.rb +++ b/spec/models/comment_notifier_spec.rb @@ -16,7 +16,7 @@ it "should be from the email address of the person who made the comment" do notifier = CommentNotifier.create_notify(@comment) notifier.from.should == [@comment.email] - notifier.from_addrs.first.name.should == @comment.name + #notifier.from_addrs.first.name.should == @comment.name end it "should say in the subject line it is a comment on a development application" do diff --git a/spec/models/notifier_spec.rb b/spec/models/notifier_spec.rb index ce291e743..a2b2fb134 100644 --- a/spec/models/notifier_spec.rb +++ b/spec/models/notifier_spec.rb @@ -15,7 +15,7 @@ it "should be from the main planningalerts email address" do @email.from.should == ["contact@planningalerts.org.au"] - @email.from_addrs.first.name.should == "PlanningAlerts.org.au" + #@email.from_addrs.first.name.should == "PlanningAlerts.org.au" end it "should say in the subject line it is an email to confirm a planning alert" do @@ -23,11 +23,11 @@ end it "should include a confirmation url" do - @email.body.should include_text("http://localhost:3000/alerts/abcdef/confirmed") + @email.should have_body_text(/http:\/\/localhost:3000\/alerts\/abcdef\/confirmed/) end it "should include the address for the alert" do - @email.body.should include_text(@alert.address) + @email.should have_body_text(/#{@alert.address}/) end end @@ -46,7 +46,7 @@ it "should be from the main planningalerts email address" do notifier = EmailConfirmable::Notifier.create_confirm(@comment) notifier.from.should == ["contact@planningalerts.org.au"] - notifier.from_addrs.first.name.should == "PlanningAlerts.org.au" + #notifier.from_addrs.first.name.should == "PlanningAlerts.org.au" end it "should say in the subject line it is an email to confirm a comment" do @@ -56,7 +56,7 @@ it "should include a confirmation url" do notifier = EmailConfirmable::Notifier.create_confirm(@comment) - notifier.body.should include_text("http://localhost:3000/comments/abcdef/confirmed") + notifier.should have_body_text(/http:\/\/localhost:3000\/comments\/abcdef\/confirmed/) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1f72de02d..9b8b02c8f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,54 +1,27 @@ -# This file is copied to ~/spec when you run 'ruby script/generate rspec' -# from the project root directory. +# This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' -require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment')) -require 'spec/autorun' -require 'spec/rails' +require File.expand_path("../../config/environment", __FILE__) +require 'rspec/rails' -# Uncomment the next line to use webrat's matchers -#require 'webrat/integrations/rspec-rails' +# Requires supporting ruby files with custom matchers and macros, etc, +# in spec/support/ and its subdirectories. +Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} -# Requires supporting files with custom matchers and macros, etc, -# in ./support/ and its subdirectories. -Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f} - -Spec::Runner.configure do |config| - # If you're not using ActiveRecord you should remove these - # lines, delete config/database.yml and disable :active_record - # in your config/boot.rb - config.use_transactional_fixtures = true - config.use_instantiated_fixtures = false - config.fixture_path = RAILS_ROOT + '/spec/fixtures/' - - # == Fixtures - # - # You can declare fixtures for each example_group like this: - # describe "...." do - # fixtures :table_a, :table_b - # - # Alternatively, if you prefer to declare them only once, you can - # do so right here. Just uncomment the next line and replace the fixture - # names with your fixtures. - # - # config.global_fixtures = :table_a, :table_b - # - # If you declare global fixtures, be aware that they will be declared - # for all of your examples, even those that don't use them. - # - # You can also declare which fixtures to use (for example fixtures for test/fixtures): - # - # config.fixture_path = RAILS_ROOT + '/spec/fixtures/' - # +RSpec.configure do |config| # == Mock Framework # - # RSpec uses its own mocking framework by default. If you prefer to - # use mocha, flexmock or RR, uncomment the appropriate line: + # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: # # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr - # - # == Notes - # - # For more information take a look at Spec::Runner::Configuration and Spec::Runner + config.mock_with :rspec + + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true end diff --git a/spec/views/applications/show_spec.rb b/spec/views/applications/show_spec.rb index f4502e2e6..7ff666d4f 100644 --- a/spec/views/applications/show_spec.rb +++ b/spec/views/applications/show_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -describe ApplicationsController do +describe "applications/show" do before :each do authority = mock_model(Authority, :full_name => "An authority", :short_name => "Blue Mountains", :contactable? => false) - assigns[:application] = mock_model(Application, :map_url => "http://a.map.url", + @application = mock_model(Application, :map_url => "http://a.map.url", :description => "A planning application", :council_reference => "A1", :authority => authority, :info_url => "http://info.url", :comment_url => "http://comment.url", :on_notice_from => nil, :on_notice_to => nil, :find_all_nearest_or_recent => [], :comments => []) errors = mock('Errors', :[] => nil) @@ -12,40 +12,43 @@ describe "show" do before :each do - assigns[:application].stub!(:address).and_return("foo") - assigns[:application].stub!(:lat).and_return(1.0) - assigns[:application].stub!(:lng).and_return(2.0) - assigns[:application].stub!(:location).and_return(Location.new(1.0, 2.0)) + @application.stub!(:address).and_return("foo") + @application.stub!(:lat).and_return(1.0) + @application.stub!(:lng).and_return(2.0) + @application.stub!(:location).and_return(Location.new(1.0, 2.0)) end it "should display the map" do - assigns[:application].stub!(:date_received).and_return(nil) - assigns[:application].stub!(:date_scraped).and_return(Time.now) - render "applications/show" - response.should have_tag("div#map_div") + @application.stub!(:date_received).and_return(nil) + @application.stub!(:date_scraped).and_return(Time.now) + assigns[:application] = @application + render + response.should have_selector("div#map_div") end it "should say nothing about notice period when there is no information" do - assigns[:application].stub!(:date_received).and_return(nil) - assigns[:application].stub!(:date_scraped).and_return(Time.now) - assigns[:application].stub!(:on_notice_from).and_return(nil) - assigns[:application].stub!(:on_notice_to).and_return(nil) - render "applications/show" - response.should_not have_tag("p.on_notice") + @application.stub!(:date_received).and_return(nil) + @application.stub!(:date_scraped).and_return(Time.now) + @application.stub!(:on_notice_from).and_return(nil) + @application.stub!(:on_notice_to).and_return(nil) + assigns[:application] = @application + render + response.should_not have_selector("p.on_notice") end end describe "show with application with no location" do it "should not display the map" do - assigns[:application].stub!(:address).and_return("An address that can't be geocoded") - assigns[:application].stub!(:lat).and_return(nil) - assigns[:application].stub!(:lng).and_return(nil) - assigns[:application].stub!(:location).and_return(nil) - assigns[:application].stub!(:date_received).and_return(nil) - assigns[:application].stub!(:date_scraped).and_return(Time.now) + @application.stub!(:address).and_return("An address that can't be geocoded") + @application.stub!(:lat).and_return(nil) + @application.stub!(:lng).and_return(nil) + @application.stub!(:location).and_return(nil) + @application.stub!(:date_received).and_return(nil) + @application.stub!(:date_scraped).and_return(Time.now) + assigns[:application] = @application - render "applications/show" - response.should_not have_tag("div#map_div") + render + response.should_not have_selector("div#map_div") end end end \ No newline at end of file diff --git a/spec/views/confirm/alert_spec.rb b/spec/views/confirm/alert_spec.rb index dfb416bfa..c804e89b1 100644 --- a/spec/views/confirm/alert_spec.rb +++ b/spec/views/confirm/alert_spec.rb @@ -1,17 +1,15 @@ require 'spec_helper' -describe EmailConfirmable::ConfirmController do - describe "distances" do - it "should be displayed in meters when under 1000m" do - assigns[:alert] = mock_model(Alert, :radius_meters => 200, :address => "An address", :confirm_id => "abcdef") - render "email_confirmable/confirm/alert" - response.should include_text("200 m") - end - - it "should be displaying in kilometers when over 1000m" do - assigns[:alert] = mock_model(Alert, :radius_meters => 3000, :address => "An address", :confirm_id => "abcdef") - render "email_confirmable/confirm/alert" - response.should include_text("3 km") - end +describe "email_confirmable/confirm/alert" do + it "should be displayed in meters when under 1000m" do + assign(:alert, mock_model(Alert, :radius_meters => 200, :address => "An address", :confirm_id => "abcdef")) + render + response.should contain("200 m") + end + + it "should be displaying in kilometers when over 1000m" do + assign(:alert, mock_model(Alert, :radius_meters => 3000, :address => "An address", :confirm_id => "abcdef")) + render + response.should contain("3 km") end end \ No newline at end of file diff --git a/spec/views/static/about_spec.rb b/spec/views/static/about_spec.rb index 514f08128..a31fa9329 100644 --- a/spec/views/static/about_spec.rb +++ b/spec/views/static/about_spec.rb @@ -1,12 +1,12 @@ require 'spec_helper' -describe StaticController, "about" do +describe "static/about.haml" do it "should show a list of the authorities" do a1 = mock_model(Authority, :full_name => "Wombat District Council", :short_name_encoded => "wombat") a2 = mock_model(Authority, :full_name => "Kangaroo City Council", :short_name_encoded => "kangaroo") - assigns[:authorities] = [["NSW", [a1, a2]]] - render "static/about" - response.should include_text(a1.full_name) - response.should include_text(a2.full_name) + assign(:authorities, [["NSW", [a1, a2]]]) + render + response.should contain(a1.full_name) + response.should contain(a2.full_name) end end \ No newline at end of file