2012-01-05 19:55:54 -05:00
|
|
|
require 'active_support/test_case'
|
2010-11-19 12:29:33 -05:00
|
|
|
require 'active_support/core_ext/class/attribute'
|
|
|
|
|
2007-10-25 22:21:21 -04:00
|
|
|
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
|
2008-01-05 08:34:15 -05:00
|
|
|
|
2007-10-25 22:21:21 -04:00
|
|
|
class TestCase < ActiveSupport::TestCase
|
2010-06-12 09:18:42 -04:00
|
|
|
module Behavior
|
|
|
|
extend ActiveSupport::Concern
|
2007-10-25 22:21:21 -04:00
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
include TestHelper
|
2008-01-05 08:34:15 -05:00
|
|
|
|
2011-11-30 12:06:07 -05:00
|
|
|
included do
|
|
|
|
class_attribute :_mailer_class
|
|
|
|
setup :initialize_test_deliveries
|
|
|
|
setup :set_expected_mail
|
|
|
|
end
|
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
module ClassMethods
|
|
|
|
def tests(mailer)
|
2011-10-03 03:04:22 -04:00
|
|
|
case mailer
|
|
|
|
when String, Symbol
|
|
|
|
self._mailer_class = mailer.to_s.camelize.constantize
|
|
|
|
when Module
|
|
|
|
self._mailer_class = mailer
|
|
|
|
else
|
|
|
|
raise NonInferrableMailerError.new(mailer)
|
|
|
|
end
|
2010-06-12 09:18:42 -04:00
|
|
|
end
|
2007-10-25 22:21:21 -04:00
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
def mailer_class
|
2010-11-19 12:29:33 -05:00
|
|
|
if mailer = self._mailer_class
|
2010-06-12 09:18:42 -04:00
|
|
|
mailer
|
|
|
|
else
|
|
|
|
tests determine_default_mailer(name)
|
|
|
|
end
|
2007-10-25 22:21:21 -04:00
|
|
|
end
|
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
def determine_default_mailer(name)
|
|
|
|
name.sub(/Test$/, '').constantize
|
2010-07-22 23:36:34 -04:00
|
|
|
rescue NameError
|
2010-06-12 09:18:42 -04:00
|
|
|
raise NonInferrableMailerError.new(name)
|
|
|
|
end
|
2007-10-25 22:21:21 -04:00
|
|
|
end
|
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
protected
|
2007-10-25 22:21:21 -04:00
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
def initialize_test_deliveries
|
|
|
|
ActionMailer::Base.delivery_method = :test
|
|
|
|
ActionMailer::Base.perform_deliveries = true
|
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_expected_mail
|
|
|
|
@expected = Mail.new
|
|
|
|
@expected.content_type ["text", "plain", { "charset" => charset }]
|
|
|
|
@expected.mime_version = '1.0'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def charset
|
|
|
|
"UTF-8"
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode(subject)
|
|
|
|
Mail::Encodings.q_value_encode(subject, charset)
|
|
|
|
end
|
2007-10-25 22:21:21 -04:00
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
def read_fixture(action)
|
|
|
|
IO.readlines(File.join(Rails.root, 'test', 'fixtures', self.class.mailer_class.name.underscore, action))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include Behavior
|
2007-10-25 22:21:21 -04:00
|
|
|
end
|
|
|
|
end
|