Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Test framework improved
Browse files Browse the repository at this point in the history
  • Loading branch information
starpeak committed May 3, 2010
1 parent f1cbc9b commit 3ed3d00
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ group :test do
gem 'rspec-rails', '>= 2.0.0.beta.7'
gem 'capybara', '>= 0.3.7'
gem 'database_cleaner', '>= 0.5.2'
gem 'cucumber', '= 0.6.4' #'> 0.7.0.beta.2' # as 0.7.2.beta.2 is not running for us
gem 'cucumber', :git => 'git://github.com/aslakhellesoy/cucumber.git'
gem 'cucumber-rails', :git => 'git://github.com/aslakhellesoy/cucumber-rails.git'
gem 'pickle', '>= 0.2.4'
gem 'capybara', '>= 0.3.5'
Expand Down
52 changes: 39 additions & 13 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@ require 'rake/testtask'
require 'rake/rdoctask'
require 'rake/gempackagetask'

namespace :db do
namespace :test do
task :prepare do
ActiveRecord::Base.configurations = Rails::Application.config.database_configuration
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
ActiveRecord::Migrator.migrate("#{File.dirname(__FILE__)}/test/rails/db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
end
desc "Remove the test database"
task :drop do
require 'pathname'
config = Rails::Application.config.database_configuration['test']
path = Pathname.new(config['database'])
file = path.absolute? ? path.to_s : File.join(Rails.root, path)

FileUtils.rm(file)
end
desc "Reset the test database and prepare it through scripts in db/migrate"
task :reset => [:drop, :prepare]
end
end

Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'HumpyardForm'
Expand All @@ -29,39 +51,43 @@ begin
spec.rcov_opts = %[--exclude "core,expectations,gems/*,spec/resources,spec/spec,spec/spec_helper.rb,/Library/Ruby/*,config/*" --text-summary --sort coverage]
end
end

task :spec => [:'db:test:reset']
rescue
task :spec do
abort 'Spec is not available. In order to run features, you must: sudo gem install rspec'
abort 'Spec is not available. In order to run cucumber, you must: sudo gem install rspec'
end
end

begin
require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features)
Cucumber::Rake::Task.new(:cucumber)

namespace :features do
desc "Run features to generate coverage"
Cucumber::Rake::Task.new(:rcov) do |features|
features.rcov = true
features.rcov_opts = %w{--rails --exclude osx\/objc,gems\/,spec\/,features\/,test\/ --aggregate coverage.data}
features.rcov_opts << %[-o "coverage"]
namespace :cucumber do
desc "Run cucumber to generate coverage"
Cucumber::Rake::Task.new(:rcov) do |cucumber|
cucumber.rcov = true
cucumber.rcov_opts = %w{--rails --exclude osx\/objc,gems\/,spec\/,features\/,test\/ --aggregate coverage.data}
cucumber.rcov_opts << %[-o "coverage"]
end

task :cucumber => [:'db:test:reset']
end
rescue LoadError
task :features do
abort 'Cucumber is not available. In order to run features, you must: sudo gem install cucumber'
task :cucumber do
abort 'Cucumber is not available. In order to run cucumber, you must: sudo gem install cucumber'
end
end

desc "Run RSpec and Cucumber tests"
task :test => [:spec, :features]
task :test => [:spec, :cucumber]
task :default => :test

desc "Run both specs and features to generate aggregated coverage"
desc "Run both rspec and cucumber tests to generate aggregated coverage"
task :rcov do |t|
rm "coverage.data" if File.exist?("coverage.data")
Rake::Task['spec:rcov'].invoke
Rake::Task["features:rcov"].invoke
Rake::Task["cucumber:rcov"].invoke
`open #{File.dirname(__FILE__)}/coverage/index.html`
end

Expand Down
20 changes: 10 additions & 10 deletions features/step_definitions/web_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def with_scope(locator)

Then /^(?:|I )should see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, selector|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
if defined?(Rspec::Rails::Matchers)
page.should have_content(text)
else
assert page.has_content?(text)
Expand All @@ -109,7 +109,7 @@ def with_scope(locator)
Then /^(?:|I )should see \/([^\/]*)\/(?: within "([^\"]*)")?$/ do |regexp, selector|
regexp = Regexp.new(regexp)
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
if defined?(Rspec::Rails::Matchers)
page.should have_xpath('//*', :text => regexp)
else
assert page.has_xpath?('//*', :text => regexp)
Expand All @@ -119,7 +119,7 @@ def with_scope(locator)

Then /^(?:|I )should not see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, selector|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
if defined?(Rspec::Rails::Matchers)
page.should have_no_content(text)
else
assert page.has_no_content?(text)
Expand All @@ -130,7 +130,7 @@ def with_scope(locator)
Then /^(?:|I )should not see \/([^\/]*)\/(?: within "([^\"]*)")?$/ do |regexp, selector|
regexp = Regexp.new(regexp)
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
if defined?(Rspec::Rails::Matchers)
page.should have_no_xpath('//*', :text => regexp)
else
assert page.has_no_xpath?('//*', :text => regexp)
Expand All @@ -140,7 +140,7 @@ def with_scope(locator)

Then /^the "([^\"]*)" field(?: within "([^\"]*)")? should contain "([^\"]*)"$/ do |field, selector, value|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
if defined?(Rspec::Rails::Matchers)
find_field(field).value.should =~ /#{value}/
else
assert_match(/#{value}/, field_labeled(field).value)
Expand All @@ -150,7 +150,7 @@ def with_scope(locator)

Then /^the "([^\"]*)" field(?: within "([^\"]*)")? should not contain "([^\"]*)"$/ do |field, selector, value|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
if defined?(Rspec::Rails::Matchers)
find_field(field).value.should_not =~ /#{value}/
else
assert_no_match(/#{value}/, find_field(field).value)
Expand All @@ -160,7 +160,7 @@ def with_scope(locator)

Then /^the "([^\"]*)" checkbox(?: within "([^\"]*)")? should be checked$/ do |label, selector|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
if defined?(Rspec::Rails::Matchers)
find_field(label)['checked'].should == 'checked'
else
assert_equal 'checked', field_labeled(label)['checked']
Expand All @@ -170,7 +170,7 @@ def with_scope(locator)

Then /^the "([^\"]*)" checkbox(?: within "([^\"]*)")? should not be checked$/ do |label, selector|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
if defined?(Rspec::Rails::Matchers)
find_field(label)['checked'].should_not == 'checked'
else
assert_not_equal 'checked', field_labeled(label)['checked']
Expand All @@ -179,7 +179,7 @@ def with_scope(locator)
end

Then /^(?:|I )should be on (.+)$/ do |page_name|
if defined?(Spec::Rails::Matchers)
if defined?(Rspec::Rails::Matchers)
URI.parse(current_url).path.should == path_to(page_name)
else
assert_equal path_to(page_name), URI.parse(current_url).path
Expand All @@ -190,7 +190,7 @@ def with_scope(locator)
actual_params = CGI.parse(URI.parse(current_url).query)
expected_params = Hash[expected_pairs.rows_hash.map{|k,v| [k,[v]]}]

if defined?(Spec::Rails::Matchers)
if defined?(Rspec::Rails::Matchers)
actual_params.should == expected_params
else
assert_equal expected_params, actual_params
Expand Down
60 changes: 42 additions & 18 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')

ENV["RAILS_ENV"] = "test"
ENV["RAILS_ENV"] ||= "test"
require File.expand_path(File.dirname(__FILE__) + "/../../test/rails/config/environment")
require 'rails/test_help'

begin
gem "test-unit"
rescue LoadError
end

require 'rspec'
require 'cucumber/formatter/unicode' # Remove this line if you don't want Cucumber Unicode support
require 'rails/test_help'
require 'cucumber/rails/rspec'
require 'cucumber/rails/world'
require 'cucumber/rails/active_record'
require 'cucumber/web/tableish'
Expand All @@ -21,6 +13,38 @@
require 'capybara/session'
require 'cucumber/rails/capybara_javascript_emulation'

# 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

# If you set this to false, any error raised from within your app will bubble
# up to your step definition and out to cucumber unless you catch it somewhere
# on the way. You can make Rails rescue errors and render error pages on a
# per-scenario basis by tagging a scenario or feature with the @allow-rescue tag.
#
# If you set this to true, Rails will rescue all errors and render error
# pages, more or less in the same way your application would behave in the
# default production environment. It's not recommended to do this for all
# of your scenarios, as this makes it hard to discover errors in your application.
ActionController::Base.allow_rescue = false

# If you set this to true, each scenario will run in a database transaction.
# You can still turn off transactions on a per-scenario basis, simply tagging
# a feature or scenario with the @no-txn tag. If you are using Capybara,
# tagging with @culerity or @javascript will also turn transactions off.
#
# If you set this to false, transactions will be off for all scenarios,
# regardless of whether you use @no-txn or not.
#
# Beware that turning transactions off will leave data in your database
# after each scenario, which can lead to hard-to-debug failures in
# 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


require "markup_validity"

begin
Expand All @@ -30,12 +54,12 @@
puts "=== The symlink public -> test/rails/public already exists ==="
end

Before do
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean

require 'factory_girl'
Dir.glob(File.join(File.dirname(__FILE__), '../../test/factories/**/*.rb')).each {|f| require f }

# How to clean your database when transactions are turned off. See
# http://github.com/bmabey/database_cleaner for more info.
if defined?(ActiveRecord::Base)
begin
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
rescue LoadError => ignore_if_database_cleaner_not_present
end
end
5 changes: 5 additions & 0 deletions features/support/factory_girl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
begin
require 'factory_girl'
Dir.glob(File.join(File.dirname(__FILE__), '../../test/factories/**/*.rb')).each {|f| require f }
rescue LoadError => factory_girl_missing
end
5 changes: 5 additions & 0 deletions spec/support/factory_girl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
begin
require 'factory_girl'
Dir.glob(File.join(File.dirname(__FILE__), '../../test/factories/**/*.rb')).each {|f| require f }
rescue LoadError => factory_girl_missing
end

0 comments on commit 3ed3d00

Please sign in to comment.