Permalink
Browse files

Add tasks to run either on Rails 2 or Rails 3.

  • Loading branch information...
1 parent d85a6f5 commit bd151a375cc52cce15ac8ad23cbec0258873a8e3 @Manfred Manfred committed Feb 24, 2011
View
22 Rakefile
@@ -5,14 +5,24 @@ require 'rake/rdoctask'
desc 'Default: run unit tests.'
task :default => :test
-desc 'Test the plugin.'
-Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
- t.libs << 'test'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
+namespace :test do
+ Rake::TestTask.new(:rails2) do |t|
+ t.libs += %w(test test/test_helper/rails2)
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+ end
+
+ desc 'Test the plugin with Rails 3.'
+ Rake::TestTask.new(:rails3) do |t|
+ t.libs += %w(test test/test_helper/rails3)
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+ end
end
+desc 'Run all tests'
+task :test => ['test:rails2', 'test:rails3']
+
desc 'Generate documentation'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
View
64 test/test_helper.rb
@@ -1,64 +0,0 @@
-module ValidationSetsTests
- module Initializer
- VENDOR_RAILS = File.expand_path('../../../../../vendor/rails', __FILE__)
- OTHER_RAILS = File.expand_path('../../../rails', __FILE__)
- PLUGIN_ROOT = File.expand_path('../../', __FILE__)
-
- def self.rails_directory
- if File.exist?(VENDOR_RAILS)
- VENDOR_RAILS
- elsif File.exist?(OTHER_RAILS)
- OTHER_RAILS
- end
- end
-
- def self.load_dependencies
- if rails_directory
- $:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
- $:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
- else
- require 'rubygems' rescue LoadError
- end
-
- require 'test/unit'
-
- require 'active_support'
- require 'active_support/test_case'
- require 'active_record'
- require 'active_record/test_case'
- require 'active_record/base' # this is needed because of dependency hell
-
- $:.unshift File.expand_path('../../lib', __FILE__)
- require File.join(PLUGIN_ROOT, 'rails', 'init')
- end
-
- def self.configure_database
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
- ActiveRecord::Migration.verbose = false
- end
-
- def self.setup_database
- ActiveRecord::Schema.define(:version => 1) do
- create_table :accounts do |t|
- t.column :fullname, :string
- t.column :email, :string
- t.column :username, :string
- t.column :password, :string
- end
- end
- end
-
- def self.teardown_database
- ActiveRecord::Base.connection.tables.each do |table|
- ActiveRecord::Base.connection.drop_table(table)
- end
- end
-
- def self.start
- load_dependencies
- configure_database
- end
- end
-end
-
-ValidationSetsTests::Initializer.start
View
28 test/test_helper/rails2/test_helper.rb
@@ -0,0 +1,28 @@
+require File.expand_path('../../shared', __FILE__)
+
+module ValidationSetsTests
+ module Initializer
+ def self.load_dependencies
+ if rails_directory
+ $:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
+ $:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
+ else
+ require 'rubygems'
+ gem 'rails', '< 3.0'
+ end
+
+ require 'test/unit'
+
+ require 'active_support'
+ require 'active_support/test_case'
+ require 'active_record'
+ require 'active_record/test_case'
+ require 'active_record/base' # this is needed because of dependency hell
+
+ $:.unshift File.expand_path('../../lib', __FILE__)
+ require File.join(PLUGIN_ROOT, 'rails', 'init')
+ end
+ end
+end
+
+ValidationSetsTests::Initializer.start
View
28 test/test_helper/rails3/test_helper.rb
@@ -0,0 +1,28 @@
+require File.expand_path('../../shared', __FILE__)
+
+module ValidationSetsTests
+ module Initializer
+ def self.load_dependencies
+ if rails_directory
+ $:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
+ $:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
+ else
+ require 'rubygems'
+ gem 'rails', '> 3.0'
+ end
+
+ require 'test/unit'
+
+ require 'active_support'
+ require 'active_support/test_case'
+ require 'active_record'
+ require 'active_record/test_case'
+ require 'active_record/base' # this is needed because of dependency hell
+
+ $:.unshift File.expand_path('../../lib', __FILE__)
+ require File.join(PLUGIN_ROOT, 'rails', 'init')
+ end
+ end
+end
+
+ValidationSetsTests::Initializer.start
View
39 test/test_helper/shared.rb
@@ -0,0 +1,39 @@
+module ValidationSetsTests
+ module Initializer
+ VENDOR_RAILS = File.expand_path('../../../../../../vendor/rails', __FILE__)
+ PLUGIN_ROOT = File.expand_path('../../../', __FILE__)
+
+ def self.rails_directory
+ if File.exist?(VENDOR_RAILS)
+ VENDOR_RAILS
+ end
+ end
+
+ def self.configure_database
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
+ ActiveRecord::Migration.verbose = false
+ end
+
+ def self.setup_database
+ ActiveRecord::Schema.define(:version => 1) do
+ create_table :accounts do |t|
+ t.column :fullname, :string
+ t.column :email, :string
+ t.column :username, :string
+ t.column :password, :string
+ end
+ end
+ end
+
+ def self.teardown_database
+ ActiveRecord::Base.connection.tables.each do |table|
+ ActiveRecord::Base.connection.drop_table(table)
+ end
+ end
+
+ def self.start
+ load_dependencies
+ configure_database
+ end
+ end
+end
View
2 test/validation_sets/validation_set_test.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../../test_helper', __FILE__)
+require 'test_helper'
class ValidationSetTest < ActiveSupport::TestCase
test "forwards missing methods to the model" do
View
23 test/validation_sets_test.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../test_helper', __FILE__)
+require 'test_helper'
class Account < ActiveRecord::Base
attr_accessor :current_password
@@ -47,7 +47,7 @@ class ValidationSetsTest < ActiveSupport::TestCase
test "global validation should always run" do
account = Account.new
assert !account.valid?
- assert account.errors.on(:fullname)
+ assert_error(account, :fullname)
account.fullname = "Patricia Herder"
assert account.valid?
@@ -58,9 +58,9 @@ class ValidationSetsTest < ActiveSupport::TestCase
account = Account.new
account.use_validation_set(:activation)
assert !account.valid?
- assert account.errors.on(:email)
- assert account.errors.on(:username)
- assert account.errors.on(:password)
+ assert_error(account, :email)
+ assert_error(account, :username)
+ assert_error(account, :password)
account.fullname = "Patricia Herder"
account.email = 'patricia@example.com'
@@ -74,8 +74,8 @@ class ValidationSetsTest < ActiveSupport::TestCase
account = Account.new
account.use_validation_set(:admin)
assert !account.valid?
- assert account.errors.on(:fullname)
- assert account.errors.on(:email)
+ assert_error(account, :fullname)
+ assert_error(account, :email)
account.fullname = "Patricia Herder"
account.email = 'patricia@example.com'
@@ -95,6 +95,15 @@ class ValidationSetsTest < ActiveSupport::TestCase
end
end
+ def assert_error(object, attribute)
+ message = "Expected #{attribute} to have a validation error"
+ if object.errors[:base].kind_of?(Array)
+ assert object.errors[attribute].empty?, message
+ else
+ assert object.errors.on(attribute), message
+ end
+ end
+
def setup
ValidationSetsTests::Initializer.setup_database
end

0 comments on commit bd151a3

Please sign in to comment.