aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2007-10-26 02:21:21 +0000
committerMichael Koziarski <michael@koziarski.com>2007-10-26 02:21:21 +0000
commit2cc0cac3efce92bf9d0e8636f2889c37ca9f57ab (patch)
tree3ad613f6c73cd9fef243f7586dfcf3b6f329105b
parent2bfd6772a4f3f9e0ff395cfd415dafbc1c9dec80 (diff)
downloadrails-2cc0cac3efce92bf9d0e8636f2889c37ca9f57ab.tar.gz
rails-2cc0cac3efce92bf9d0e8636f2889c37ca9f57ab.tar.bz2
rails-2cc0cac3efce92bf9d0e8636f2889c37ca9f57ab.zip
Introduce TestCase subclasses for testing rails applications allowing tests to be DRY'd up a bit and to provide a path toward tidying up our monkeypatching of test/unit.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8022 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionmailer/CHANGELOG2
-rw-r--r--actionmailer/lib/action_mailer/test_case.rb59
-rw-r--r--actionmailer/test/abstract_unit.rb1
-rw-r--r--actionmailer/test/test_helper_test.rb35
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_controller/test_case.rb53
-rw-r--r--actionpack/test/controller/test_test.rb32
-rw-r--r--activesupport/CHANGELOG5
-rw-r--r--activesupport/lib/active_support.rb2
-rw-r--r--activesupport/lib/active_support/test_case.rb5
-rw-r--r--activesupport/lib/active_support/testing.rb1
-rw-r--r--activesupport/lib/active_support/testing/default.rb11
-rw-r--r--railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb12
-rw-r--r--railties/lib/rails_generator/generators/components/mailer/templates/unit_test.rb32
-rw-r--r--railties/lib/rails_generator/generators/components/model/templates/unit_test.rb2
-rw-r--r--railties/lib/test_help.rb4
16 files changed, 218 insertions, 40 deletions
diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG
index c7fadfed39..b4122e7072 100644
--- a/actionmailer/CHANGELOG
+++ b/actionmailer/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Introduce a new base test class for testing Mailers. ActionMailer::TestCase [Koz]
+
* Fix silent failure of rxml templates. #9879 [jstewart]
diff --git a/actionmailer/lib/action_mailer/test_case.rb b/actionmailer/lib/action_mailer/test_case.rb
new file mode 100644
index 0000000000..5d0ddddce9
--- /dev/null
+++ b/actionmailer/lib/action_mailer/test_case.rb
@@ -0,0 +1,59 @@
+require 'active_support/test_case'
+
+module ActionMailer
+ class NonInferrableMailerError < ::StandardError
+ def initialize(name)
+ super "Unable to determine the mailer to test from #{name}. " +
+ "You'll need to specify it using tests YourMailer in your " +
+ "test case definition"
+ end
+ end
+ # New Test Super class for forward compatibility.
+ # To override
+ class TestCase < ActiveSupport::TestCase
+ include ActionMailer::Quoting
+
+ class << self
+ def tests(mailer)
+ write_inheritable_attribute(:mailer_class, mailer)
+ end
+
+ def mailer_class
+ if mailer = read_inheritable_attribute(:mailer_class)
+ mailer
+ else
+ tests determine_default_mailer(name)
+ end
+ end
+
+ def determine_default_mailer(name)
+ name.sub(/Test$/, '').constantize
+ rescue NameError => e
+ raise NonInferrableMailerError.new(name)
+ end
+ end
+
+ def setup
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.perform_deliveries = true
+ ActionMailer::Base.deliveries = []
+
+ @expected = TMail::Mail.new
+ @expected.set_content_type "text", "plain", { "charset" => charset }
+ @expected.mime_version = '1.0'
+ end
+
+ private
+ def charset
+ "utf-8"
+ end
+
+ def encode(subject)
+ quoted_printable(subject, charset)
+ end
+
+ def read_fixture(action)
+ IO.readlines(File.join(RAILS_ROOT, 'test', 'fixtures', mailer_class.name.underscore, action))
+ end
+ end
+end
diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb
index 649e7982cc..0a73b1b462 100644
--- a/actionmailer/test/abstract_unit.rb
+++ b/actionmailer/test/abstract_unit.rb
@@ -2,6 +2,7 @@ require 'test/unit'
$:.unshift "#{File.dirname(__FILE__)}/../lib"
require 'action_mailer'
+require 'action_mailer/test_case'
# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true
diff --git a/actionmailer/test/test_helper_test.rb b/actionmailer/test/test_helper_test.rb
index 08f92865ca..eb17e3e803 100644
--- a/actionmailer/test/test_helper_test.rb
+++ b/actionmailer/test/test_helper_test.rb
@@ -8,13 +8,38 @@ class TestHelperMailer < ActionMailer::Base
end
end
-class TestHelperTest < Test::Unit::TestCase
- def setup
- ActionMailer::Base.delivery_method = :test
- ActionMailer::Base.perform_deliveries = true
- ActionMailer::Base.deliveries = []
+class TestHelperMailerTest < ActionMailer::TestCase
+
+ def test_setup_sets_right_action_mailer_options
+ assert_equal :test, ActionMailer::Base.delivery_method
+ assert ActionMailer::Base.perform_deliveries
+ assert_equal [], ActionMailer::Base.deliveries
+ end
+
+ def test_setup_creates_the_expected_mailer
+ assert @expected.is_a?(TMail::Mail)
+ assert_equal "1.0", @expected.mime_version
+ assert_equal "text/plain", @expected.content_type
+ end
+
+ def test_mailer_class_is_correctly_inferred
+ assert_equal TestHelperMailer, self.class.mailer_class
+ end
+
+ def test_determine_default_mailer_raises_correct_error
+ assert_raises(ActionMailer::NonInferrableMailerError) do
+ self.class.determine_default_mailer("NotAMailerTest")
+ end
end
+ def test_charset_is_utf_8
+ assert_equal "utf-8", charset
+ end
+
+ def test_encode
+ assert_equal "=?utf-8?Q?=0aasdf=0a?=", encode("\nasdf\n")
+ end
+
def test_assert_emails
assert_nothing_raised do
assert_emails 1 do
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 33f2d3d8da..19224a9b49 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Introduce a new test case class for functional tests. ActionController::TestCase. [Koz]
+
* Fix incorrect path in helper rdoc. Closes #9926 [viktor tron]
* Partials also set 'object' to the default partial variable. #8823 [Nick Retallack, Jeremy Kemper]
diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb
new file mode 100644
index 0000000000..35aad0d8bf
--- /dev/null
+++ b/actionpack/lib/action_controller/test_case.rb
@@ -0,0 +1,53 @@
+require 'active_support/test_case'
+
+module ActionController
+ class NonInferrableControllerError < ActionControllerError
+ def initialize(name)
+ super "Unable to determine the controller to test from #{name}. " +
+ "You'll need to specify it using tests YourController in your " +
+ "test case definition"
+ end
+ end
+
+ class TestCase < ActiveSupport::TestCase
+ @@controller_class = nil
+ class << self
+ def tests(controller_class)
+ self.controller_class = controller_class
+ end
+
+ def controller_class=(new_class)
+ prepare_controller_class(new_class)
+ write_inheritable_attribute(:controller_class, new_class)
+ end
+
+ def controller_class
+ if current_controller_class = read_inheritable_attribute(:controller_class)
+ current_controller_class
+ else
+ self.controller_class= determine_default_controller_class(name)
+ end
+ end
+
+ def determine_default_controller_class(name)
+ name.sub(/Test$/, '').constantize
+ rescue NameError
+ raise NonInferrableControllerError.new(name)
+ end
+
+ def prepare_controller_class(new_class)
+ new_class.class_eval do
+ def rescue_action(e)
+ raise e
+ end
+ end
+ end
+ end
+
+ def setup
+ @controller = self.class.controller_class.new
+ @request = TestRequest.new
+ @response = TestResponse.new
+ end
+ end
+end \ No newline at end of file
diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb
index 305c5b6931..8fbe980eec 100644
--- a/actionpack/test/controller/test_test.rb
+++ b/actionpack/test/controller/test_test.rb
@@ -1,5 +1,6 @@
require "#{File.dirname(__FILE__)}/../abstract_unit"
require "#{File.dirname(__FILE__)}/fake_controllers"
+require "action_controller/test_case"
class TestTest < Test::Unit::TestCase
class TestController < ActionController::Base
@@ -580,3 +581,34 @@ class CleanBacktraceTest < Test::Unit::TestCase
assert !caught.backtrace.empty?
end
end
+
+class InferringClassNameTest < Test::Unit::TestCase
+ def test_determine_controller_class
+ assert_equal ContentController, determine_class("ContentControllerTest")
+ end
+
+ def test_determine_controller_class_with_nonsense_name
+ assert_raises ActionController::NonInferrableControllerError do
+ determine_class("HelloGoodBye")
+ end
+ end
+
+ def test_determine_controller_class_with_sensible_name_where_no_controller_exists
+ assert_raises ActionController::NonInferrableControllerError do
+ determine_class("NoControllerWithThisNameTest")
+ end
+ end
+
+ private
+ def determine_class(name)
+ ActionController::TestCase.determine_default_controller_class(name)
+ end
+end
+
+class CrazyNameTest < ActionController::TestCase
+ tests ContentController
+ def test_controller_class_can_be_set_manually_not_just_inferred
+ assert_equal ContentController, self.class.controller_class
+ end
+end
+
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index a513cd570b..3b05196e68 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,10 @@
*SVN*
+* Introduce a base class for all test cases used by rails applications. ActiveSupport::TestCase [Koz]
+
+ The intention is to use this to reduce the amount of monkeypatching / overriding that
+ is done to test/unit's classes.
+
* Document Enumerable and Hash #to_json. #9970 [Chu Yeow]
* Hash#to_xml handles symbol values. #9954 [Assaf]
diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb
index e33f6ceb27..0b3816ec01 100644
--- a/activesupport/lib/active_support.rb
+++ b/activesupport/lib/active_support.rb
@@ -45,3 +45,5 @@ require 'active_support/json'
require 'active_support/multibyte'
+require 'active_support/testing'
+
diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb
new file mode 100644
index 0000000000..be8f8b17fc
--- /dev/null
+++ b/activesupport/lib/active_support/test_case.rb
@@ -0,0 +1,5 @@
+module ActiveSupport
+ class TestCase < Test::Unit::TestCase
+ include ActiveSupport::Testing::Default
+ end
+end \ No newline at end of file
diff --git a/activesupport/lib/active_support/testing.rb b/activesupport/lib/active_support/testing.rb
new file mode 100644
index 0000000000..1bf30cbbdd
--- /dev/null
+++ b/activesupport/lib/active_support/testing.rb
@@ -0,0 +1 @@
+require 'active_support/testing/default' \ No newline at end of file
diff --git a/activesupport/lib/active_support/testing/default.rb b/activesupport/lib/active_support/testing/default.rb
new file mode 100644
index 0000000000..0cb0eea7d2
--- /dev/null
+++ b/activesupport/lib/active_support/testing/default.rb
@@ -0,0 +1,11 @@
+module ActiveSupport
+ module Testing
+ module Default
+ def run(*args)
+ return if method_name == :default_test
+ super
+ end
+ end
+ end
+end
+
diff --git a/railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb b/railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb
index abe9c4cf8c..202a017a40 100644
--- a/railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb
+++ b/railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb
@@ -1,15 +1,7 @@
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
-require '<%= file_path %>_controller'
-# Re-raise errors caught by the controller.
-class <%= class_name %>Controller; def rescue_action(e) raise e end; end
-
-class <%= class_name %>ControllerTest < Test::Unit::TestCase
- def setup
- @controller = <%= class_name %>Controller.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
- end
+class <%= class_name %>ControllerTest < ActionController::TestCase
+ tests <%= class_name %>Controller
# Replace this with your real tests.
def test_truth
diff --git a/railties/lib/rails_generator/generators/components/mailer/templates/unit_test.rb b/railties/lib/rails_generator/generators/components/mailer/templates/unit_test.rb
index 8cb816efc0..dcd0206211 100644
--- a/railties/lib/rails_generator/generators/components/mailer/templates/unit_test.rb
+++ b/railties/lib/rails_generator/generators/components/mailer/templates/unit_test.rb
@@ -1,21 +1,7 @@
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
-class <%= class_name %>Test < Test::Unit::TestCase
- FIXTURES_PATH = File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../fixtures'
- CHARSET = "utf-8"
-
- include ActionMailer::Quoting
-
- def setup
- ActionMailer::Base.delivery_method = :test
- ActionMailer::Base.perform_deliveries = true
- ActionMailer::Base.deliveries = []
-
- @expected = TMail::Mail.new
- @expected.set_content_type "text", "plain", { "charset" => CHARSET }
- @expected.mime_version = '1.0'
- end
-
+class <%= class_name %>Test < ActionMailer::TestCase
+ tests <%= class_name %>
<% for action in actions -%>
def test_<%= action %>
@expected.subject = '<%= class_name %>#<%= action %>'
@@ -26,12 +12,10 @@ class <%= class_name %>Test < Test::Unit::TestCase
end
<% end -%>
- private
- def read_fixture(action)
- IO.readlines("#{FIXTURES_PATH}/<%= file_path %>/#{action}")
- end
-
- def encode(subject)
- quoted_printable(subject, CHARSET)
- end
+<% if actions.blank? -%>
+ # replace this with your real tests
+ def test_truth
+ assert true
+ end
+<% end -%>
end
diff --git a/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb b/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb
index ec8010c8ae..9bb3ca4160 100644
--- a/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb
+++ b/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
-class <%= class_name %>Test < Test::Unit::TestCase
+class <%= class_name %>Test < ActiveSupport::TestCase
# Replace this with your real tests.
def test_truth
assert true
diff --git a/railties/lib/test_help.rb b/railties/lib/test_help.rb
index a7401e6e93..13bac2d8d4 100644
--- a/railties/lib/test_help.rb
+++ b/railties/lib/test_help.rb
@@ -5,9 +5,13 @@ require_dependency 'application'
silence_warnings { RAILS_ENV = "test" }
require 'test/unit'
+require 'active_support/test_case'
require 'active_record/fixtures'
+require 'active_record/test_case'
+require 'action_controller/test_case'
require 'action_controller/test_process'
require 'action_controller/integration'
+require 'action_mailer/test_case'
Test::Unit::TestCase.fixture_path = RAILS_ROOT + "/test/fixtures/"
ActionController::IntegrationTest.fixture_path = Test::Unit::TestCase.fixture_path