aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-11-05 21:56:18 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-11-05 21:56:18 +0000
commitba553f94248340b54b32cee1fcbd41295713d180 (patch)
tree232174d19991632ef0583cb685d5f3c520a510cd /actionmailer
parent2fbe0ae7a2d945dbd9d0e0abad23afde75db67fb (diff)
downloadrails-ba553f94248340b54b32cee1fcbd41295713d180.tar.gz
rails-ba553f94248340b54b32cee1fcbd41295713d180.tar.bz2
rails-ba553f94248340b54b32cee1fcbd41295713d180.zip
Add assert_emails and assert_no_emails to test the number of emails delivered. Closes #6479.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5436 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionmailer')
-rw-r--r--actionmailer/CHANGELOG11
-rwxr-xr-xactionmailer/lib/action_mailer.rb1
-rw-r--r--actionmailer/lib/action_mailer/test_helper.rb67
-rw-r--r--actionmailer/test/fixtures/helper_mailer/use_example_helper.rhtml1
-rw-r--r--actionmailer/test/fixtures/helper_mailer/use_test_helper.rhtml1
-rw-r--r--actionmailer/test/fixtures/helpers/example_helper.rb (renamed from actionmailer/test/fixtures/helpers/test_helper.rb)4
-rw-r--r--actionmailer/test/mail_helper_test.rb8
-rw-r--r--actionmailer/test/test_helper_test.rb79
8 files changed, 165 insertions, 7 deletions
diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG
index 165d28652a..c876c05333 100644
--- a/actionmailer/CHANGELOG
+++ b/actionmailer/CHANGELOG
@@ -1,5 +1,16 @@
*SVN*
+* Add assert_emails and assert_no_emails to test the number of emails delivered. #6479 [Jonathan Viney]
+ # Assert total number of emails delivered:
+ assert_emails 0
+ ContactMailer.deliver_contact
+ assert_emails 1
+
+ # Assert number of emails delivered within a block:
+ assert_emails 1 do
+ post :signup, :name => 'Jonathan'
+ end
+
* Make mime version default to 1.0. closes #2323 [ror@andreas-s.net]
* Make sure quoted-printable text is decoded correctly when only portions of the text are encoded. closes #3154. [jon@siliconcircus.com]
diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb
index d17772209b..5cf9f4d9a3 100755
--- a/actionmailer/lib/action_mailer.rb
+++ b/actionmailer/lib/action_mailer.rb
@@ -37,6 +37,7 @@ require 'action_mailer/base'
require 'action_mailer/helpers'
require 'action_mailer/mail_helper'
require 'action_mailer/quoting'
+require 'action_mailer/test_helper'
require 'tmail'
require 'net/smtp'
diff --git a/actionmailer/lib/action_mailer/test_helper.rb b/actionmailer/lib/action_mailer/test_helper.rb
new file mode 100644
index 0000000000..3a1612442f
--- /dev/null
+++ b/actionmailer/lib/action_mailer/test_helper.rb
@@ -0,0 +1,67 @@
+module ActionMailer
+ module TestHelper
+ # Asserts that the number of emails sent matches the given number.
+ #
+ # def test_emails
+ # assert_emails 0
+ # ContactMailer.deliver_contact
+ # assert_emails 1
+ # ContactMailer.deliver_contact
+ # assert_emails 2
+ # end
+ #
+ # If a block is passed, that block should cause the specified number of emails to be sent.
+ #
+ # def test_emails_again
+ # assert_emails 1 do
+ # ContactMailer.deliver_contact
+ # end
+ #
+ # assert_emails 2 do
+ # ContactMailer.deliver_contact
+ # ContactMailer.deliver_contact
+ # end
+ # end
+ def assert_emails(number)
+ if block_given?
+ original_count = ActionMailer::Base.deliveries.size
+ yield
+ new_count = ActionMailer::Base.deliveries.size
+ assert_equal original_count + number, new_count, "#{number} emails expected, but #{new_count - original_count} were sent"
+ else
+ assert_equal number, ActionMailer::Base.deliveries.size
+ end
+ end
+
+ # Assert that no emails have been sent.
+ #
+ # def test_emails
+ # assert_no_emails
+ # ContactMailer.deliver_contact
+ # assert_emails 1
+ # end
+ #
+ # If a block is passed, that block should not cause any emails to be sent.
+ #
+ # def test_emails_again
+ # assert_no_emails do
+ # # No emails should be sent from this block
+ # end
+ # end
+ #
+ # Note: This assertion is simply a shortcut for:
+ #
+ # assert_emails 0
+ def assert_no_emails(&block)
+ assert_emails 0, &block
+ end
+ end
+end
+
+module Test
+ module Unit
+ class TestCase
+ include ActionMailer::TestHelper
+ end
+ end
+end
diff --git a/actionmailer/test/fixtures/helper_mailer/use_example_helper.rhtml b/actionmailer/test/fixtures/helper_mailer/use_example_helper.rhtml
new file mode 100644
index 0000000000..fcff3bb1b4
--- /dev/null
+++ b/actionmailer/test/fixtures/helper_mailer/use_example_helper.rhtml
@@ -0,0 +1 @@
+So, <%= example_format(@text) %>
diff --git a/actionmailer/test/fixtures/helper_mailer/use_test_helper.rhtml b/actionmailer/test/fixtures/helper_mailer/use_test_helper.rhtml
deleted file mode 100644
index 52ea9aa413..0000000000
--- a/actionmailer/test/fixtures/helper_mailer/use_test_helper.rhtml
+++ /dev/null
@@ -1 +0,0 @@
-So, <%= test_format(@text) %>
diff --git a/actionmailer/test/fixtures/helpers/test_helper.rb b/actionmailer/test/fixtures/helpers/example_helper.rb
index f479820c96..d66927aa60 100644
--- a/actionmailer/test/fixtures/helpers/test_helper.rb
+++ b/actionmailer/test/fixtures/helpers/example_helper.rb
@@ -1,5 +1,5 @@
-module TestHelper
- def test_format(text)
+module ExampleHelper
+ def example_format(text)
"<em><strong><small>#{text}</small></strong></em>"
end
end
diff --git a/actionmailer/test/mail_helper_test.rb b/actionmailer/test/mail_helper_test.rb
index 19f3707db8..344dcd7c49 100644
--- a/actionmailer/test/mail_helper_test.rb
+++ b/actionmailer/test/mail_helper_test.rb
@@ -8,7 +8,7 @@ end
class HelperMailer < ActionMailer::Base
helper MailerHelper
- helper :test
+ helper :example
def use_helper(recipient)
recipients recipient
@@ -16,7 +16,7 @@ class HelperMailer < ActionMailer::Base
from "tester@example.com"
end
- def use_test_helper(recipient)
+ def use_example_helper(recipient)
recipients recipient
subject "using helpers"
from "tester@example.com"
@@ -72,8 +72,8 @@ class MailerHelperTest < Test::Unit::TestCase
assert_match %r{Mr. Joe Person}, mail.encoded
end
- def test_use_test_helper
- mail = HelperMailer.create_use_test_helper(@recipient)
+ def test_use_example_helper
+ mail = HelperMailer.create_use_example_helper(@recipient)
assert_match %r{<em><strong><small>emphasize me!}, mail.encoded
end
diff --git a/actionmailer/test/test_helper_test.rb b/actionmailer/test/test_helper_test.rb
new file mode 100644
index 0000000000..a22e7a5991
--- /dev/null
+++ b/actionmailer/test/test_helper_test.rb
@@ -0,0 +1,79 @@
+require File.dirname(__FILE__) + '/abstract_unit'
+
+class TestHelperMailer < ActionMailer::Base
+ def test
+ recipients "test@example.com"
+ from "tester@example.com"
+ body render(:inline => "Hello, <%= @world %>", :body => { :world => "Earth" })
+ end
+end
+
+class TestHelperTest < Test::Unit::TestCase
+ def setup
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.perform_deliveries = true
+ ActionMailer::Base.deliveries = []
+ end
+
+ def test_assert_emails
+ assert_nothing_raised do
+ assert_emails 1 do
+ TestHelperMailer.deliver_test
+ end
+ end
+ end
+
+ def test_repeated_assert_emails_calls
+ assert_nothing_raised do
+ assert_emails 1 do
+ TestHelperMailer.deliver_test
+ end
+ end
+
+ assert_nothing_raised do
+ assert_emails 2 do
+ TestHelperMailer.deliver_test
+ TestHelperMailer.deliver_test
+ end
+ end
+ end
+
+ def test_assert_no_emails
+ assert_nothing_raised do
+ assert_no_emails do
+ TestHelperMailer.create_test
+ end
+ end
+ end
+
+ def test_assert_emails_too_few_sent
+ error = assert_raises Test::Unit::AssertionFailedError do
+ assert_emails 2 do
+ TestHelperMailer.deliver_test
+ end
+ end
+
+ assert_match /2 .* but 1/, error.message
+ end
+
+ def test_assert_emails_too_many_sent
+ error = assert_raises Test::Unit::AssertionFailedError do
+ assert_emails 1 do
+ TestHelperMailer.deliver_test
+ TestHelperMailer.deliver_test
+ end
+ end
+
+ assert_match /1 .* but 2/, error.message
+ end
+
+ def test_assert_no_emails_failure
+ error = assert_raises Test::Unit::AssertionFailedError do
+ assert_no_emails do
+ TestHelperMailer.deliver_test
+ end
+ end
+
+ assert_match /0 .* but 1/, error.message
+ end
+end