Permalink
Browse files

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
  • Loading branch information...
1 parent 2bfd677 commit 2cc0cac3efce92bf9d0e8636f2889c37ca9f57ab @NZKoz NZKoz committed Oct 26, 2007
View
2 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]
View
59 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
View
1 actionmailer/test/abstract_unit.rb
@@ -2,6 +2,7 @@
$:.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
View
35 actionmailer/test/test_helper_test.rb
@@ -8,13 +8,38 @@ def test
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
View
2 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]
View
53 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
View
32 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 @@ def test_should_only_clean_assertion_failure_errors
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
+
View
5 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]
View
2 activesupport/lib/active_support.rb
@@ -45,3 +45,5 @@
require 'active_support/multibyte'
+require 'active_support/testing'
+
View
5 activesupport/lib/active_support/test_case.rb
@@ -0,0 +1,5 @@
+module ActiveSupport
+ class TestCase < Test::Unit::TestCase
+ include ActiveSupport::Testing::Default
+ end
+end
View
1 activesupport/lib/active_support/testing.rb
@@ -0,0 +1 @@
+require 'active_support/testing/default'
View
11 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
+
View
12 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
View
32 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 @@ def test_<%= action %>
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
View
2 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
View
4 railties/lib/test_help.rb
@@ -5,9 +5,13 @@
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

0 comments on commit 2cc0cac

Please sign in to comment.