From 2cc0cac3efce92bf9d0e8636f2889c37ca9f57ab Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Fri, 26 Oct 2007 02:21:21 +0000 Subject: 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 --- actionmailer/CHANGELOG | 2 + actionmailer/lib/action_mailer/test_case.rb | 59 +++++++++++++++++++++++++++++ actionmailer/test/abstract_unit.rb | 1 + actionmailer/test/test_helper_test.rb | 35 ++++++++++++++--- 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 actionmailer/lib/action_mailer/test_case.rb (limited to 'actionmailer') 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 -- cgit v1.2.3