Add tests to mail helper.

This commit is contained in:
José Valim and Mikel Lindsaar 2010-01-24 19:52:44 +01:00
parent bd96614101
commit a74a655648
11 changed files with 86 additions and 141 deletions

View File

@ -256,7 +256,6 @@ module ActionMailer #:nodoc:
include DeliveryMethods, Quoting
abstract!
# TODO Add some sanity tests for the included modules
include AbstractController::Logger
include AbstractController::Rendering
include AbstractController::LocalizedCache

View File

@ -17,12 +17,12 @@ module ActionMailer
end
# Access the mailer instance.
def mailer #:nodoc:
def mailer
@_controller
end
# Access the message instance.
def message #:nodoc:
def message
@_message
end
end

View File

@ -19,7 +19,6 @@ ActionView::Template.register_template_handler :bak, lambda { |template| "Lame b
FIXTURE_LOAD_PATH = File.expand_path('fixtures', File.dirname(__FILE__))
ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH
$:.unshift File.join(FIXTURE_LOAD_PATH, 'helpers')
class MockSMTP
def self.deliveries

View File

@ -71,7 +71,7 @@ class CustomDeliveryMethodsTest < ActiveSupport::TestCase
end
class MailDeliveryTest < ActiveSupport::TestCase
class DeliverMail < ActionMailer::Base
class DeliveryMailer < ActionMailer::Base
DEFAULT_HEADERS = {
:to => 'mikel@test.lindsaar.net',
:from => 'jose@test.plataformatec.com'
@ -87,64 +87,64 @@ class MailDeliveryTest < ActiveSupport::TestCase
end
def teardown
DeliverMail.delivery_method = :smtp
DeliverMail.perform_deliveries = true
DeliverMail.raise_delivery_errors = true
DeliveryMailer.delivery_method = :smtp
DeliveryMailer.perform_deliveries = true
DeliveryMailer.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)
DeliveryMailer.deliveries.clear
DeliveryMailer.expects(:delivered_email).once
DeliveryMailer.welcome.deliver
assert_equal(1, DeliveryMailer.deliveries.length)
end
test "delivery method can be customized per instance" do
email = DeliverMail.welcome.deliver
email = DeliveryMailer.welcome.deliver
assert_instance_of Mail::SMTP, email.delivery_method
email = DeliverMail.welcome(:delivery_method => :test).deliver
email = DeliveryMailer.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
DeliveryMailer.delivery_method = :test
assert_equal :smtp, ActionMailer::Base.delivery_method
$BREAK = true
email = DeliverMail.welcome.deliver
email = DeliveryMailer.welcome.deliver
assert_instance_of Mail::TestMailer, email.delivery_method
end
test "non registered delivery methods raises errors" do
DeliverMail.delivery_method = :unknown
DeliveryMailer.delivery_method = :unknown
assert_raise RuntimeError do
DeliverMail.welcome.deliver
DeliveryMailer.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)
DeliveryMailer.perform_deliveries = false
DeliveryMailer.deliveries.clear
DeliveryMailer.expects(:delivered_email).never
DeliveryMailer.welcome.deliver
assert_equal(0, DeliveryMailer.deliveries.length)
end
test "raise errors on bogus deliveries" do
DeliverMail.delivery_method = BogusDelivery
DeliverMail.deliveries.clear
DeliverMail.expects(:delivered_email).never
DeliveryMailer.delivery_method = BogusDelivery
DeliveryMailer.deliveries.clear
DeliveryMailer.expects(:delivered_email).never
assert_raise RuntimeError do
DeliverMail.welcome.deliver
DeliveryMailer.welcome.deliver
end
assert_equal(0, DeliverMail.deliveries.length)
assert_equal(0, DeliveryMailer.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)
DeliveryMailer.delivery_method = BogusDelivery
DeliveryMailer.raise_delivery_errors = false
DeliveryMailer.deliveries.clear
DeliveryMailer.expects(:delivered_email).once
DeliveryMailer.welcome.deliver
assert_equal(1, DeliveryMailer.deliveries.length)
end
end

View File

@ -1 +0,0 @@
So, <%= example_format(@text) %>

View File

@ -1 +0,0 @@
Hello, <%= person_name %>. Thanks for registering!

View File

@ -1 +0,0 @@
This message brought to you by <%= name_of_the_mailer_class %>.

View File

@ -1,5 +0,0 @@
From "Romeo and Juliet":
<%= block_format @text %>
Good ol' Shakespeare.

View File

@ -1,5 +0,0 @@
module ExampleHelper
def example_format(text)
"<em><strong><small>#{h(text)}</small></strong></em>".html_safe!
end
end

View File

@ -0,0 +1,54 @@
require 'abstract_unit'
class HelperMailer < ActionMailer::Base
def use_mail_helper
@text = "But soft! What light through yonder window breaks? It is the east, " +
"and Juliet is the sun. Arise, fair sun, and kill the envious moon, " +
"which is sick and pale with grief that thou, her maid, art far more " +
"fair than she. Be not her maid, for she is envious! Her vestal " +
"livery is but sick and green, and none but fools do wear it. Cast " +
"it off!"
mail_with_defaults do |format|
format.html { render(:inline => "<%= block_format @text %>") }
end
end
def use_mailer
mail_with_defaults do |format|
format.html { render(:inline => "<%= mailer.message.subject %>") }
end
end
def use_message
mail_with_defaults do |format|
format.html { render(:inline => "<%= message.subject %>") }
end
end
protected
def mail_with_defaults(&block)
mail(:to => "test@localhost", :from => "tester@example.com",
:subject => "using helpers", &block)
end
end
class MailerHelperTest < ActionMailer::TestCase
def test_use_mail_helper
mail = HelperMailer.use_mail_helper
assert_match %r{ But soft!}, mail.body.encoded
assert_match %r{east, and\r\n Juliet}, mail.body.encoded
end
def test_use_mailer
mail = HelperMailer.use_mailer
assert_match "using helpers", mail.body.encoded
end
def test_use_message
mail = HelperMailer.use_message
assert_match "using helpers", mail.body.encoded
end
end

View File

@ -1,94 +0,0 @@
require 'abstract_unit'
module MailerHelper
def person_name
"Mr. Joe Person"
end
end
class HelperMailer < ActionMailer::Base
helper MailerHelper
helper :example
def use_helper
recipients 'test@localhost'
subject "using helpers"
from "tester@example.com"
end
def use_example_helper
recipients 'test@localhost'
subject "using helpers"
from "tester@example.com"
@text = "emphasize me!"
end
def use_mail_helper
recipients 'test@localhost'
subject "using mailing helpers"
from "tester@example.com"
@text = "But soft! What light through yonder window breaks? It is the east, " +
"and Juliet is the sun. Arise, fair sun, and kill the envious moon, " +
"which is sick and pale with grief that thou, her maid, art far more " +
"fair than she. Be not her maid, for she is envious! Her vestal " +
"livery is but sick and green, and none but fools do wear it. Cast " +
"it off!"
end
def use_helper_method
recipients 'test@localhost'
subject "using helpers"
from "tester@example.com"
@text = "emphasize me!"
end
private
def name_of_the_mailer_class
self.class.name
end
helper_method :name_of_the_mailer_class
end
class MailerHelperTest < ActiveSupport::TestCase
def new_mail( charset="utf-8" )
mail = Mail.new
mail.set_content_type "text", "plain", { "charset" => charset } if charset
mail
end
def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries.clear
end
def teardown
restore_delivery_method
end
def test_use_helper
mail = HelperMailer.use_helper
assert_match %r{Mr. Joe Person}, mail.encoded
end
def test_use_example_helper
mail = HelperMailer.use_example_helper
assert_match %r{<em><strong><small>emphasize me!}, mail.encoded
end
def test_use_helper_method
mail = HelperMailer.use_helper_method
assert_match %r{HelperMailer}, mail.encoded
end
def test_use_mail_helper
mail = HelperMailer.use_mail_helper
assert_match %r{ But soft!}, mail.encoded
assert_match %r{east, and\r\n Juliet}, mail.encoded
end
end