2009-06-25 10:21:33 -04:00
|
|
|
require 'generators/generators_test_helper'
|
2010-04-30 06:11:38 -04:00
|
|
|
require 'rails/generators/mailer/mailer_generator'
|
2010-04-29 19:45:45 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class MailerGeneratorTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
2010-01-03 10:34:32 -05:00
|
|
|
arguments %w(notifier foo bar)
|
2009-06-25 10:21:33 -04:00
|
|
|
|
|
|
|
def test_mailer_skeleton_is_created
|
|
|
|
run_generator
|
2010-01-25 07:13:29 -05:00
|
|
|
assert_file "app/mailers/notifier.rb" do |mailer|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/class Notifier < ActionMailer::Base/, mailer)
|
2011-12-20 11:58:45 -05:00
|
|
|
assert_match(/default from: "from@example.com"/, mailer)
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mailer_with_i18n_helper
|
|
|
|
run_generator
|
|
|
|
assert_file "app/mailers/notifier.rb" do |mailer|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/en\.notifier\.foo\.subject/, mailer)
|
|
|
|
assert_match(/en\.notifier\.bar\.subject/, mailer)
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
2009-06-25 10:21:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_check_class_collision
|
2013-12-16 00:52:58 -05:00
|
|
|
Object.send :const_set, :Notifier, Class.new
|
|
|
|
content = capture(:stderr){ run_generator }
|
|
|
|
assert_match(/The name 'Notifier' is either already used in your application or reserved/, content)
|
|
|
|
ensure
|
|
|
|
Object.send :remove_const, :Notifier
|
2009-06-25 10:21:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invokes_default_test_framework
|
|
|
|
run_generator
|
2012-10-08 00:59:42 -04:00
|
|
|
assert_file "test/mailers/notifier_test.rb" do |test|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/class NotifierTest < ActionMailer::TestCase/, test)
|
|
|
|
assert_match(/test "foo"/, test)
|
|
|
|
assert_match(/test "bar"/, test)
|
2010-03-22 14:54:14 -04:00
|
|
|
end
|
2013-12-22 05:06:12 -05:00
|
|
|
assert_file "test/mailers/previews/notifier_preview.rb" do |preview|
|
|
|
|
assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/notifier/, preview)
|
|
|
|
assert_match(/class NotifierPreview < ActionMailer::Preview/, preview)
|
|
|
|
assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier\/foo/, preview)
|
|
|
|
assert_instance_method :foo, preview do |foo|
|
2013-12-16 00:52:58 -05:00
|
|
|
assert_match(/Notifier.foo/, foo)
|
|
|
|
end
|
2013-12-22 05:06:12 -05:00
|
|
|
assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier\/bar/, preview)
|
|
|
|
assert_instance_method :bar, preview do |bar|
|
2013-12-16 00:52:58 -05:00
|
|
|
assert_match(/Notifier.bar/, bar)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_check_test_class_collision
|
|
|
|
Object.send :const_set, :NotifierTest, Class.new
|
|
|
|
content = capture(:stderr){ run_generator }
|
|
|
|
assert_match(/The name 'NotifierTest' is either already used in your application or reserved/, content)
|
|
|
|
ensure
|
|
|
|
Object.send :remove_const, :NotifierTest
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_check_preview_class_collision
|
|
|
|
Object.send :const_set, :NotifierPreview, Class.new
|
|
|
|
content = capture(:stderr){ run_generator }
|
|
|
|
assert_match(/The name 'NotifierPreview' is either already used in your application or reserved/, content)
|
|
|
|
ensure
|
|
|
|
Object.send :remove_const, :NotifierPreview
|
2009-06-25 10:21:33 -04:00
|
|
|
end
|
|
|
|
|
2013-12-19 19:12:55 -05:00
|
|
|
def test_invokes_default_text_template_engine
|
2009-06-25 10:21:33 -04:00
|
|
|
run_generator
|
2010-01-25 19:06:48 -05:00
|
|
|
assert_file "app/views/notifier/foo.text.erb" do |view|
|
2014-02-14 11:17:12 -05:00
|
|
|
assert_match(%r(\sapp/views/notifier/foo\.text\.erb), view)
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/<%= @greeting %>/, view)
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
2010-01-25 19:06:48 -05:00
|
|
|
assert_file "app/views/notifier/bar.text.erb" do |view|
|
2014-02-14 11:17:12 -05:00
|
|
|
assert_match(%r(\sapp/views/notifier/bar\.text\.erb), view)
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/<%= @greeting %>/, view)
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
2009-06-25 10:21:33 -04:00
|
|
|
end
|
|
|
|
|
2013-12-19 19:12:55 -05:00
|
|
|
def test_invokes_default_html_template_engine
|
|
|
|
run_generator
|
|
|
|
assert_file "app/views/notifier/foo.html.erb" do |view|
|
2014-02-14 11:17:12 -05:00
|
|
|
assert_match(%r(\sapp/views/notifier/foo\.html\.erb), view)
|
2013-12-19 19:12:55 -05:00
|
|
|
assert_match(/<%= @greeting %>/, view)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_file "app/views/notifier/bar.html.erb" do |view|
|
2014-02-14 11:17:12 -05:00
|
|
|
assert_match(%r(\sapp/views/notifier/bar\.html\.erb), view)
|
2013-12-19 19:12:55 -05:00
|
|
|
assert_match(/<%= @greeting %>/, view)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-25 10:21:33 -04:00
|
|
|
def test_invokes_default_template_engine_even_with_no_action
|
|
|
|
run_generator ["notifier"]
|
|
|
|
assert_file "app/views/notifier"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_logs_if_the_template_engine_cannot_be_found
|
2009-07-08 06:55:50 -04:00
|
|
|
content = run_generator ["notifier", "foo", "bar", "--template-engine=haml"]
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/haml \[not found\]/, content)
|
2009-06-25 10:21:33 -04:00
|
|
|
end
|
|
|
|
|
2010-10-06 02:29:11 -04:00
|
|
|
def test_mailer_with_namedspaced_mailer
|
|
|
|
run_generator ["Farm::Animal", "moos"]
|
|
|
|
assert_file "app/mailers/farm/animal.rb" do |mailer|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/class Farm::Animal < ActionMailer::Base/, mailer)
|
|
|
|
assert_match(/en\.farm\.animal\.moos\.subject/, mailer)
|
2010-10-06 02:29:11 -04:00
|
|
|
end
|
2013-12-22 05:06:12 -05:00
|
|
|
assert_file "test/mailers/previews/farm/animal_preview.rb" do |preview|
|
|
|
|
assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal/, preview)
|
|
|
|
assert_match(/class Farm::AnimalPreview < ActionMailer::Preview/, preview)
|
|
|
|
assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal\/moos/, preview)
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
2010-10-06 02:29:11 -04:00
|
|
|
assert_file "app/views/farm/animal/moos.text.erb"
|
2013-12-19 19:12:55 -05:00
|
|
|
assert_file "app/views/farm/animal/moos.html.erb"
|
2010-10-06 02:29:11 -04:00
|
|
|
end
|
|
|
|
|
2009-06-25 10:27:44 -04:00
|
|
|
def test_actions_are_turned_into_methods
|
|
|
|
run_generator
|
2010-01-25 07:13:29 -05:00
|
|
|
|
|
|
|
assert_file "app/mailers/notifier.rb" do |mailer|
|
|
|
|
assert_instance_method :foo, mailer do |foo|
|
2011-12-20 11:51:26 -05:00
|
|
|
assert_match(/mail to: "to@example.org"/, foo)
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/@greeting = "Hi"/, foo)
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_instance_method :bar, mailer do |bar|
|
2011-12-20 11:51:26 -05:00
|
|
|
assert_match(/mail to: "to@example.org"/, bar)
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/@greeting = "Hi"/, bar)
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
end
|
2011-05-13 00:28:38 -04:00
|
|
|
end
|
2009-06-25 10:21:33 -04:00
|
|
|
end
|