2012-01-05 19:55:54 -05:00
|
|
|
require 'active_support/test_case'
|
2014-07-15 12:20:14 -04:00
|
|
|
require 'rails-dom-testing'
|
2010-11-19 12:29:33 -05:00
|
|
|
|
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
|
|
|
|
2012-09-25 18:04:09 -04:00
|
|
|
include ActiveSupport::Testing::ConstantLookup
|
2010-06-12 09:18:42 -04:00
|
|
|
include TestHelper
|
2014-07-15 12:20:14 -04:00
|
|
|
include Rails::Dom::Testing::Assertions::SelectorAssertions
|
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
|
2014-06-06 12:11:24 -04:00
|
|
|
teardown :restore_test_deliveries
|
2011-11-30 12:06:07 -05:00
|
|
|
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)
|
2012-09-24 16:46:58 -04:00
|
|
|
mailer = determine_constant_from_test_name(name) do |constant|
|
|
|
|
Class === constant && constant < ActionMailer::Base
|
|
|
|
end
|
|
|
|
raise NonInferrableMailerError.new(name) if mailer.nil?
|
|
|
|
mailer
|
2010-06-12 09:18:42 -04:00
|
|
|
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
|
2014-05-28 06:55:01 -04:00
|
|
|
set_delivery_method :test
|
2014-06-06 12:11:24 -04:00
|
|
|
@old_perform_deliveries = ActionMailer::Base.perform_deliveries
|
2010-06-12 09:18:42 -04:00
|
|
|
ActionMailer::Base.perform_deliveries = true
|
2014-06-06 12:11:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def restore_test_deliveries
|
2014-05-28 06:55:01 -04:00
|
|
|
restore_delivery_method
|
2014-06-06 12:11:24 -04:00
|
|
|
ActionMailer::Base.perform_deliveries = @old_perform_deliveries
|
2010-06-12 09:18:42 -04:00
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
end
|
|
|
|
|
2014-08-19 22:26:45 -04:00
|
|
|
def set_delivery_method(method)
|
|
|
|
@old_delivery_method = ActionMailer::Base.delivery_method
|
|
|
|
ActionMailer::Base.delivery_method = method
|
|
|
|
end
|
|
|
|
|
|
|
|
def restore_delivery_method
|
|
|
|
ActionMailer::Base.delivery_method = @old_delivery_method
|
|
|
|
end
|
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
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
|