diff options
author | Rafael Mendonça França <rafael.franca@plataformatec.com.br> | 2014-08-17 22:38:22 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafael.franca@plataformatec.com.br> | 2014-08-17 22:38:22 -0300 |
commit | c78da4d5c472f7254ed609ef753d7b1719732802 (patch) | |
tree | f04630ac62d08a49744b90bdf20bf75dcb883838 /actionmailer/test | |
parent | 4e4913462104df89f1bee08faeb4ba5aab8c9228 (diff) | |
parent | 7ee055076ec2c5b2e9c110f821c10fe06bf38a1c (diff) | |
download | rails-c78da4d5c472f7254ed609ef753d7b1719732802.tar.gz rails-c78da4d5c472f7254ed609ef753d7b1719732802.tar.bz2 rails-c78da4d5c472f7254ed609ef753d7b1719732802.zip |
Merge branch 'master' into loofah
Conflicts:
actionpack/CHANGELOG.md
Diffstat (limited to 'actionmailer/test')
-rw-r--r-- | actionmailer/test/abstract_unit.rb | 5 | ||||
-rw-r--r-- | actionmailer/test/base_test.rb | 8 | ||||
-rw-r--r-- | actionmailer/test/mailers/delayed_mailer.rb | 6 | ||||
-rw-r--r-- | actionmailer/test/message_delivery_test.rb | 82 |
4 files changed, 93 insertions, 8 deletions
diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb index 28727b62ec..c549545674 100644 --- a/actionmailer/test/abstract_unit.rb +++ b/actionmailer/test/abstract_unit.rb @@ -51,8 +51,3 @@ def jruby_skip(message = '') end require 'mocha/setup' # FIXME: stop using mocha - -# FIXME: we have tests that depend on run order, we should fix that and -# remove this method call. -require 'active_support/test_case' -ActiveSupport::TestCase.my_tests_are_order_dependent! diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 6116d1e29f..fc24639bf4 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -11,6 +11,8 @@ require 'mailers/asset_mailer' class BaseTest < ActiveSupport::TestCase setup do + @original_delivery_method = ActionMailer::Base.delivery_method + ActionMailer::Base.delivery_method = :test @original_asset_host = ActionMailer::Base.asset_host @original_assets_dir = ActionMailer::Base.assets_dir end @@ -19,6 +21,7 @@ class BaseTest < ActiveSupport::TestCase ActionMailer::Base.asset_host = @original_asset_host ActionMailer::Base.assets_dir = @original_assets_dir BaseMailer.deliveries.clear + ActionMailer::Base.delivery_method = @original_delivery_method end test "method call to mail does not raise error" do @@ -240,7 +243,7 @@ class BaseTest < ActiveSupport::TestCase end end - e = assert_raises(RuntimeError) { LateAttachmentMailer.welcome } + e = assert_raises(RuntimeError) { LateAttachmentMailer.welcome.message } assert_match(/Can't add attachments after `mail` was called./, e.message) end @@ -252,7 +255,7 @@ class BaseTest < ActiveSupport::TestCase end end - e = assert_raises(RuntimeError) { LateInlineAttachmentMailer.welcome } + e = assert_raises(RuntimeError) { LateInlineAttachmentMailer.welcome.message } assert_match(/Can't add attachments after `mail` was called./, e.message) end @@ -468,7 +471,6 @@ class BaseTest < ActiveSupport::TestCase end test "calling deliver on the action should increment the deliveries collection if using the test mailer" do - BaseMailer.delivery_method = :test BaseMailer.welcome.deliver assert_equal(1, BaseMailer.deliveries.length) end diff --git a/actionmailer/test/mailers/delayed_mailer.rb b/actionmailer/test/mailers/delayed_mailer.rb new file mode 100644 index 0000000000..62d4baa434 --- /dev/null +++ b/actionmailer/test/mailers/delayed_mailer.rb @@ -0,0 +1,6 @@ +class DelayedMailer < ActionMailer::Base + + def test_message(*) + mail(from: 'test-sender@test.com', to: 'test-receiver@test.com', subject: 'Test Subject', body: 'Test Body') + end +end diff --git a/actionmailer/test/message_delivery_test.rb b/actionmailer/test/message_delivery_test.rb new file mode 100644 index 0000000000..a097d0e84d --- /dev/null +++ b/actionmailer/test/message_delivery_test.rb @@ -0,0 +1,82 @@ +# encoding: utf-8 +gem 'activejob' +require 'active_job' +require 'abstract_unit' +require 'minitest/mock' +require_relative 'mailers/delayed_mailer' + +class MessageDeliveryTest < ActiveSupport::TestCase + + setup do + @previous_logger = ActiveJob::Base.logger + @previous_delivery_method = ActionMailer::Base.delivery_method + ActionMailer::Base.delivery_method = :test + ActiveJob::Base.logger = Logger.new(nil) + @mail = DelayedMailer.test_message(1, 2, 3) + ActionMailer::Base.deliveries.clear + end + + teardown do + ActiveJob::Base.logger = @previous_logger + ActionMailer::Base.delivery_method = @previous_delivery_method + end + + test 'should have a message' do + assert @mail.message + end + + test 'its message should be a Mail::Message' do + assert_equal Mail::Message , @mail.message.class + end + + test 'should respond to .deliver' do + assert_respond_to @mail, :deliver + end + + test 'should respond to .deliver!' do + assert_respond_to @mail, :deliver! + end + + test 'should respond to .deliver_later' do + assert_respond_to @mail, :deliver_later + end + + test 'should respond to .deliver_later!' do + assert_respond_to @mail, :deliver_later! + end + + test 'should enqueue and run correctly in activejob' do + @mail.deliver_later! + assert_equal 1 , ActionMailer::Base.deliveries.size + end + + test 'should enqueue the email with :deliver delivery method' do + ret = ActionMailer::DeliveryJob.stub :enqueue, ->(*args){ args } do + @mail.deliver_later + end + assert_equal ['DelayedMailer', 'test_message', 'deliver', 1, 2, 3], ret + end + + test 'should enqueue the email with :deliver! delivery method' do + ret = ActionMailer::DeliveryJob.stub :enqueue, ->(*args){ args } do + @mail.deliver_later! + end + assert_equal ['DelayedMailer', 'test_message', 'deliver!', 1, 2, 3], ret + end + + test 'should enqueue a delivery with a delay' do + ret = ActionMailer::DeliveryJob.stub :enqueue_in, ->(*args){ args } do + @mail.deliver_later in: 600 + end + assert_equal [600, 'DelayedMailer', 'test_message', 'deliver', 1, 2, 3], ret + end + + test 'should enqueue a delivery at a specific time' do + later_time = Time.now.to_i + 3600 + ret = ActionMailer::DeliveryJob.stub :enqueue_at, ->(*args){ args } do + @mail.deliver_later at: later_time + end + assert_equal [later_time, 'DelayedMailer', 'test_message', 'deliver', 1, 2, 3], ret + end + +end |