Sha256: b0f97e431bd1a82b93f237cfa605734ee6c50829a2f487bb41b017e9e7e84669

Contents?: true

Size: 1.6 KB

Versions: 14

Compression:

Stored size: 1.6 KB

Contents

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

  class TestCase < ActiveSupport::TestCase
    include ActionMailer::Quoting

    setup :initialize_test_deliveries
    setup :set_expected_mail

    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

    protected
      def initialize_test_deliveries
        ActionMailer::Base.delivery_method = :test
        ActionMailer::Base.perform_deliveries = true
        ActionMailer::Base.deliveries = []
      end

      def set_expected_mail
        @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', self.class.mailer_class.name.underscore, action))
      end
  end
end

Version data entries

14 entries across 13 versions & 5 rubygems

Version Path
radiant-0.7.2 vendor/rails/actionmailer/lib/action_mailer/test_case.rb
vibes-bj-1.2.2 spec/rails_root/vendor/rails/actionmailer/lib/action_mailer/test_case.rb
vibes-bj-1.2.1 spec/rails_root/vendor/rails/actionmailer/lib/action_mailer/test_case.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/actionmailer/lib/action_mailer/test_case.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/actionmailer/pkg/actionmailer-2.2.2/lib/action_mailer/test_case.rb
actionmailer-2.2.3 lib/action_mailer/test_case.rb
actionmailer-2.1.1 lib/action_mailer/test_case.rb
actionmailer-2.1.0 lib/action_mailer/test_case.rb
actionmailer-2.1.2 lib/action_mailer/test_case.rb
actionmailer-2.2.2 lib/action_mailer/test_case.rb
antfarm-0.3.0 rails/vendor/rails/actionmailer/lib/action_mailer/test_case.rb
antfarm-0.4.0 rails/vendor/rails/actionmailer/lib/action_mailer/test_case.rb
radiant-0.7.0 vendor/rails/actionmailer/lib/action_mailer/test_case.rb
radiant-0.7.1 vendor/rails/actionmailer/lib/action_mailer/test_case.rb