add test coverage for the action mailer

This commit is contained in:
Dmitry Polushkin 2014-02-10 13:24:27 +00:00
parent a8e1538b60
commit 99f8d4feeb
6 changed files with 16 additions and 38 deletions

View File

@ -26,27 +26,9 @@ I18n.enforce_available_locales = false
FIXTURE_LOAD_PATH = File.expand_path('fixtures', File.dirname(__FILE__))
ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH
class MockSMTP
def self.deliveries
@@deliveries
end
def initialize
@@deliveries = []
end
def sendmail(mail, from, to)
@@deliveries << [mail, from, to]
end
def start(*args)
yield self
end
end
class Net::SMTP
def self.new(*args)
MockSMTP.new
class Rails
def self.root
File.expand_path('../', File.dirname(__FILE__))
end
end

View File

@ -31,25 +31,10 @@ class AssetHostTest < ActiveSupport::TestCase
def test_asset_host_as_one_argument_proc
AssetHostMailer.config.asset_host = Proc.new { |source|
if source.starts_with?('/images')
"http://images.example.com"
else
"http://assets.example.com"
'http://images.example.com'
end
}
mail = AssetHostMailer.email_with_asset
assert_equal %Q{<img alt="Somelogo" src="http://images.example.com/images/somelogo.png" />}, mail.body.to_s.strip
end
def test_asset_host_as_two_argument_proc
ActionController::Base.config.asset_host = Proc.new {|source,request|
if request && request.ssl?
"https://www.example.com"
else
"http://www.example.com"
end
}
mail = nil
assert_nothing_raised { mail = AssetHostMailer.email_with_asset }
assert_equal %Q{<img alt="Somelogo" src="http://www.example.com/images/somelogo.png" />}, mail.body.to_s.strip
end
end

View File

@ -108,6 +108,7 @@ class MailDeliveryTest < ActiveSupport::TestCase
end
test "delivery method can be customized per instance" do
Mail::SMTP.any_instance.expects(:deliver!)
email = DeliveryMailer.welcome.deliver
assert_instance_of Mail::SMTP, email.delivery_method
email = DeliveryMailer.welcome(delivery_method: :test).deliver
@ -117,7 +118,6 @@ class MailDeliveryTest < ActiveSupport::TestCase
test "delivery method can be customized in subclasses not changing the parent" do
DeliveryMailer.delivery_method = :test
assert_equal :smtp, ActionMailer::Base.delivery_method
$BREAK = true
email = DeliveryMailer.welcome.deliver
assert_instance_of Mail::TestMailer, email.delivery_method
end

View File

@ -0,0 +1 @@
Welcome!

View File

@ -37,6 +37,7 @@ class ActionMailerI18nWithControllerTest < ActionDispatch::IntegrationTest
end
def test_send_mail
Mail::SMTP.any_instance.expects(:deliver!)
with_translation 'de', email_subject: '[Anmeldung] Willkommen' do
get '/test/send_mail'
assert_equal "Mail sent - Subject: [Anmeldung] Willkommen", @response.body

View File

@ -1,3 +1,4 @@
# encoding: utf-8
require 'abstract_unit'
class TestHelperMailer < ActionMailer::Base
@ -36,6 +37,14 @@ class TestHelperMailerTest < ActionMailer::TestCase
assert_equal "UTF-8", charset
end
def test_encode
assert_equal '=?UTF-8?Q?This_is_=E3=81=82_string?=', encode('This is あ string')
end
def test_read_fixture
assert_equal ['Welcome!'], read_fixture('welcome')
end
def test_assert_emails
assert_nothing_raised do
assert_emails 1 do