Permalink
Browse files

Move old tests to a specific folder and add some delivery method tests.

  • Loading branch information...
1 parent 99f960a commit bd96614101262e0ad0cc176ed8e2d95a5c17936b José Valim and Mikel Lindsaar committed Jan 24, 2010
@@ -22,14 +22,14 @@ task :default => [ :test ]
# Run the unit tests
Rake::TestTask.new { |t|
t.libs << "test"
- t.pattern = 'test/*_test.rb'
+ t.pattern = 'test/**/*_test.rb'
t.warning = true
}
namespace :test do
task :isolated do
ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME'))
- Dir.glob("test/*_test.rb").all? do |file|
+ Dir.glob("test/**/*_test.rb").all? do |file|
system(ruby, '-Ilib:test', file)
end or raise "Failures"
end
@@ -256,6 +256,7 @@ class Base < AbstractController::Base
include DeliveryMethods, Quoting
abstract!
+ # TODO Add some sanity tests for the included modules
include AbstractController::Logger
include AbstractController::Rendering
include AbstractController::LocalizedCache
@@ -270,12 +271,6 @@ class Base < AbstractController::Base
private_class_method :new #:nodoc:
- cattr_accessor :raise_delivery_errors
- @@raise_delivery_errors = true
-
- cattr_accessor :perform_deliveries
- @@perform_deliveries = true
-
extlib_inheritable_accessor :default_charset
self.default_charset = "utf-8"
@@ -295,9 +290,6 @@ class Base < AbstractController::Base
self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ]
class << self
- # Provides a list of emails that have been delivered by Mail
- delegate :deliveries, :deliveries=, :to => Mail
-
def mailer_name
@mailer_name ||= name.underscore
end
@@ -1,14 +1,22 @@
require 'tmpdir'
module ActionMailer
- # Provides a DSL for adding delivery methods to ActionMailer.
+ # This modules handles everything related to the delivery, from registering new
+ # delivery methods to configuring the mail object to be send.
module DeliveryMethods
extend ActiveSupport::Concern
included do
extlib_inheritable_accessor :delivery_methods, :delivery_method,
:instance_writer => false
+ # Do not make this inheritable, because we always want it to propagate
+ cattr_accessor :raise_delivery_errors
+ self.raise_delivery_errors = true
+
+ cattr_accessor :perform_deliveries
+ self.perform_deliveries = true
+
self.delivery_methods = {}
self.delivery_method = :smtp
@@ -32,6 +40,9 @@ module DeliveryMethods
end
module ClassMethods
+ # Provides a list of emails that have been delivered by Mail
+ delegate :deliveries, :deliveries=, :to => Mail
+
# Adds a new delivery method through the given class using the given symbol
# as alias and the default options supplied:
#
@@ -50,7 +61,8 @@ def add_delivery_method(symbol, klass, default_options={})
self.delivery_methods[symbol.to_sym] = klass
end
- def wrap_delivery_behavior(mail, method=delivery_method) #:nodoc:
+ def wrap_delivery_behavior(mail, method=nil) #:nodoc:
+ method ||= self.delivery_method
mail.register_for_delivery_notification(self)
if method.is_a?(Symbol)
@@ -23,7 +23,7 @@ def mailer #:nodoc:
# Access the message instance.
def message #:nodoc:
- @message
+ @_message
end
end
end
@@ -8,7 +8,6 @@
require 'rubygems'
require 'test/unit'
-
require 'action_mailer'
# Show backtraces for deprecated behavior for quicker cleanup.
@@ -18,15 +17,11 @@
ActionView::Template.register_template_handler :haml, lambda { |template| "Look its HAML!".inspect }
ActionView::Template.register_template_handler :bak, lambda { |template| "Lame backup".inspect }
-ActionView::Base::DEFAULT_CONFIG = { :assets_dir => '/nowhere' }
-
-$:.unshift "#{File.dirname(__FILE__)}/fixtures/helpers"
+FIXTURE_LOAD_PATH = File.expand_path('fixtures', File.dirname(__FILE__))
+ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH
+$:.unshift File.join(FIXTURE_LOAD_PATH, 'helpers')
-FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
-ActionMailer::Base.template_root = FIXTURE_LOAD_PATH
-
-class MockSMTP
-
+class MockSMTP
def self.deliveries
@@deliveries
end
@@ -42,7 +37,6 @@ def sendmail(mail, from, to)
def start(*args)
yield self
end
-
end
class Net::SMTP
@@ -51,19 +45,11 @@ def self.new(*args)
end
end
-def uses_gem(gem_name, test_name, version = '> 0')
- gem gem_name.to_s, version
- require gem_name.to_s
- yield
-rescue LoadError
- $stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
-end
-
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
+end
@@ -310,14 +310,6 @@ def explicit_multipart_with_any(hash = {})
assert_equal(1, BaseMailer.deliveries.length)
end
- # Delivery hooks
- test "ActionMailer should be told when Mail gets delivered" do
- BaseMailer.deliveries.clear
- BaseMailer.expects(:delivered_email).once
- BaseMailer.welcome.deliver
- assert_equal(1, BaseMailer.deliveries.length)
- end
-
protected
# Execute the block setting the given values and restoring old values after
@@ -4,20 +4,17 @@
class MyCustomDelivery
end
-class DefaultsDeliveryMethodsTest < ActionMailer::TestCase
- def setup
- set_delivery_method :smtp
+class BogusDelivery
+ def initialize(*)
end
- def teardown
- restore_delivery_method
- end
-
- def test_should_be_the_default_smtp
- assert_equal :smtp, ActionMailer::Base.delivery_method
+ def deliver!(mail)
+ raise "failed"
end
+end
- def test_should_have_default_smtp_delivery_method_settings
+class DefaultsDeliveryMethodsTest < ActiveSupport::TestCase
+ test "default smtp settings" do
settings = { :address => "localhost",
:port => 25,
:domain => 'localhost.localdomain',
@@ -28,45 +25,126 @@ def test_should_have_default_smtp_delivery_method_settings
assert_equal settings, ActionMailer::Base.smtp_settings
end
- def test_should_have_default_file_delivery_method_settings
+ test "default file delivery settings" do
settings = {:location => "#{Dir.tmpdir}/mails"}
assert_equal settings, ActionMailer::Base.file_settings
end
- def test_should_have_default_sendmail_delivery_method_settings
+ test "default sendmail settings" do
settings = {:location => '/usr/sbin/sendmail',
:arguments => '-i -t'}
assert_equal settings, ActionMailer::Base.sendmail_settings
end
end
-class CustomDeliveryMethodsTest < ActionMailer::TestCase
+class CustomDeliveryMethodsTest < ActiveSupport::TestCase
def setup
+ @old_delivery_method = ActionMailer::Base.delivery_method
ActionMailer::Base.add_delivery_method :custom, MyCustomDelivery
end
def teardown
+ ActionMailer::Base.delivery_method = @old_delivery_method
ActionMailer::Base.delivery_methods.delete(:custom)
end
- def test_allow_to_add_a_custom_delivery_method
+ test "allow to add custom delivery method" do
ActionMailer::Base.delivery_method = :custom
assert_equal :custom, ActionMailer::Base.delivery_method
end
- def test_allow_to_customize_custom_settings
+ test "allow to customize custom settings" do
ActionMailer::Base.custom_settings = { :foo => :bar }
assert_equal Hash[:foo => :bar], ActionMailer::Base.custom_settings
end
- def test_respond_to_custom_method_settings
+ test "respond to custom settings" do
assert_respond_to ActionMailer::Base, :custom_settings
assert_respond_to ActionMailer::Base, :custom_settings=
end
- def test_should_not_respond_for_invalid_method_settings
+ test "does not respond to unknown settings" do
assert_raise NoMethodError do
ActionMailer::Base.another_settings
end
end
end
+
+class MailDeliveryTest < ActiveSupport::TestCase
+ class DeliverMail < ActionMailer::Base
+ DEFAULT_HEADERS = {
+ :to => 'mikel@test.lindsaar.net',
+ :from => 'jose@test.plataformatec.com'
+ }
+
+ def welcome(hash={})
+ mail(DEFAULT_HEADERS.merge(hash))
+ end
+ end
+
+ def setup
+ ActionMailer::Base.delivery_method = :smtp
+ end
+
+ def teardown
+ DeliverMail.delivery_method = :smtp
+ DeliverMail.perform_deliveries = true
+ DeliverMail.raise_delivery_errors = true
+ end
+
+ test "ActionMailer should be told when Mail gets delivered" do
+ DeliverMail.deliveries.clear
+ DeliverMail.expects(:delivered_email).once
+ DeliverMail.welcome.deliver
+ assert_equal(1, DeliverMail.deliveries.length)
+ end
+
+ test "delivery method can be customized per instance" do
+ email = DeliverMail.welcome.deliver
+ assert_instance_of Mail::SMTP, email.delivery_method
+ email = DeliverMail.welcome(:delivery_method => :test).deliver
+ assert_instance_of Mail::TestMailer, email.delivery_method
+ end
+
+ test "delivery method can be customized in subclasses not changing the parent" do
+ DeliverMail.delivery_method = :test
+ assert_equal :smtp, ActionMailer::Base.delivery_method
+ $BREAK = true
+ email = DeliverMail.welcome.deliver
+ assert_instance_of Mail::TestMailer, email.delivery_method
+ end
+
+ test "non registered delivery methods raises errors" do
+ DeliverMail.delivery_method = :unknown
+ assert_raise RuntimeError do
+ DeliverMail.welcome.deliver
+ end
+ end
+
+ test "does not perform deliveries if requested" do
+ DeliverMail.perform_deliveries = false
+ DeliverMail.deliveries.clear
+ DeliverMail.expects(:delivered_email).never
+ DeliverMail.welcome.deliver
+ assert_equal(0, DeliverMail.deliveries.length)
+ end
+
+ test "raise errors on bogus deliveries" do
+ DeliverMail.delivery_method = BogusDelivery
+ DeliverMail.deliveries.clear
+ DeliverMail.expects(:delivered_email).never
+ assert_raise RuntimeError do
+ DeliverMail.welcome.deliver
+ end
+ assert_equal(0, DeliverMail.deliveries.length)
+ end
+
+ test "does not raise errors on bogus deliveries if set" do
+ DeliverMail.delivery_method = BogusDelivery
+ DeliverMail.raise_delivery_errors = false
+ DeliverMail.deliveries.clear
+ DeliverMail.expects(:delivered_email).once
+ DeliverMail.welcome.deliver
+ assert_equal(1, DeliverMail.deliveries.length)
+ end
+end
@@ -8,7 +8,6 @@ def test_body
quoted_body = [expected].pack('*M')
m.body = quoted_body
assert_equal "something_with_underscores=\r\n", m.body.encoded
- # CHANGED: body returns object, not string, Changed m.body to m.body.to_s
assert_equal expected, m.body.to_s
end
@@ -20,5 +19,4 @@ def test_nested_attachments_are_recognized_correctly
assert_equal 1902, mail.attachments.first.decoded.length
assert_equal "application/pkcs7-signature", mail.attachments.last.mime_type
end
-
end
Oops, something went wrong.

0 comments on commit bd96614

Please sign in to comment.