Froze rails gems
[depot.git] / vendor / rails / actionmailer / lib / action_mailer / test_case.rb
1 require 'active_support/test_case'
2
3 module ActionMailer
4 class NonInferrableMailerError < ::StandardError
5 def initialize(name)
6 super "Unable to determine the mailer to test from #{name}. " +
7 "You'll need to specify it using tests YourMailer in your " +
8 "test case definition"
9 end
10 end
11
12 class TestCase < ActiveSupport::TestCase
13 include ActionMailer::Quoting
14
15 setup :initialize_test_deliveries
16 setup :set_expected_mail
17
18 class << self
19 def tests(mailer)
20 write_inheritable_attribute(:mailer_class, mailer)
21 end
22
23 def mailer_class
24 if mailer = read_inheritable_attribute(:mailer_class)
25 mailer
26 else
27 tests determine_default_mailer(name)
28 end
29 end
30
31 def determine_default_mailer(name)
32 name.sub(/Test$/, '').constantize
33 rescue NameError => e
34 raise NonInferrableMailerError.new(name)
35 end
36 end
37
38 protected
39 def initialize_test_deliveries
40 ActionMailer::Base.delivery_method = :test
41 ActionMailer::Base.perform_deliveries = true
42 ActionMailer::Base.deliveries = []
43 end
44
45 def set_expected_mail
46 @expected = TMail::Mail.new
47 @expected.set_content_type "text", "plain", { "charset" => charset }
48 @expected.mime_version = '1.0'
49 end
50
51 private
52 def charset
53 "utf-8"
54 end
55
56 def encode(subject)
57 quoted_printable(subject, charset)
58 end
59
60 def read_fixture(action)
61 IO.readlines(File.join(RAILS_ROOT, 'test', 'fixtures', self.class.mailer_class.name.underscore, action))
62 end
63 end
64 end