2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "generators/generators_test_helper"
|
|
|
|
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
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_file "app/mailers/notifier_mailer.rb" do |mailer|
|
|
|
|
assert_match(/class NotifierMailer < ApplicationMailer/, mailer)
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_no_match(/default from: "from@example\.com"/, mailer)
|
2014-11-17 05:44:04 -05:00
|
|
|
assert_no_match(/layout :mailer_notifier/, mailer)
|
|
|
|
end
|
2016-03-11 19:43:37 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_file "app/mailers/application_mailer.rb" do |mailer|
|
2016-03-11 19:43:37 -05:00
|
|
|
assert_match(/class ApplicationMailer < ActionMailer::Base/, mailer)
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/default from: 'from@example\.com'/, mailer)
|
2016-03-21 13:07:38 -04:00
|
|
|
assert_match(/layout 'mailer'/, mailer)
|
2016-03-11 19:43:37 -05:00
|
|
|
end
|
2014-11-17 05:44:04 -05:00
|
|
|
end
|
|
|
|
|
2010-01-25 07:13:29 -05:00
|
|
|
def test_mailer_with_i18n_helper
|
|
|
|
run_generator
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_file "app/mailers/notifier_mailer.rb" do |mailer|
|
2015-01-08 22:24:22 -05:00
|
|
|
assert_match(/en\.notifier_mailer\.foo\.subject/, mailer)
|
|
|
|
assert_match(/en\.notifier_mailer\.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
|
2014-12-17 13:18:22 -05:00
|
|
|
Object.send :const_set, :NotifierMailer, Class.new
|
2016-08-16 03:30:11 -04:00
|
|
|
content = capture(:stderr) { run_generator }
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_match(/The name 'NotifierMailer' is either already used in your application or reserved/, content)
|
2013-12-16 00:52:58 -05:00
|
|
|
ensure
|
2014-12-17 13:18:22 -05:00
|
|
|
Object.send :remove_const, :NotifierMailer
|
2009-06-25 10:21:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invokes_default_test_framework
|
|
|
|
run_generator
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_file "test/mailers/notifier_mailer_test.rb" do |test|
|
|
|
|
assert_match(/class NotifierMailerTest < ActionMailer::TestCase/, test)
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/test "foo"/, test)
|
|
|
|
assert_match(/test "bar"/, test)
|
2010-03-22 14:54:14 -04:00
|
|
|
end
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_file "test/mailers/previews/notifier_mailer_preview.rb" do |preview|
|
2015-03-14 19:57:06 -04:00
|
|
|
assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/notifier_mailer/, preview)
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_match(/class NotifierMailerPreview < ActionMailer::Preview/, preview)
|
2015-03-14 19:57:06 -04:00
|
|
|
assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier_mailer\/foo/, preview)
|
2013-12-22 05:06:12 -05:00
|
|
|
assert_instance_method :foo, preview do |foo|
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/NotifierMailer\.foo/, foo)
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
2015-03-14 19:57:06 -04:00
|
|
|
assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier_mailer\/bar/, preview)
|
2013-12-22 05:06:12 -05:00
|
|
|
assert_instance_method :bar, preview do |bar|
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/NotifierMailer\.bar/, bar)
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_check_test_class_collision
|
2014-12-17 13:18:22 -05:00
|
|
|
Object.send :const_set, :NotifierMailerTest, Class.new
|
2016-08-16 03:30:11 -04:00
|
|
|
content = capture(:stderr) { run_generator }
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_match(/The name 'NotifierMailerTest' is either already used in your application or reserved/, content)
|
2013-12-16 00:52:58 -05:00
|
|
|
ensure
|
2014-12-17 13:18:22 -05:00
|
|
|
Object.send :remove_const, :NotifierMailerTest
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_check_preview_class_collision
|
2014-12-17 13:18:22 -05:00
|
|
|
Object.send :const_set, :NotifierMailerPreview, Class.new
|
2016-08-16 03:30:11 -04:00
|
|
|
content = capture(:stderr) { run_generator }
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_match(/The name 'NotifierMailerPreview' is either already used in your application or reserved/, content)
|
2013-12-16 00:52:58 -05:00
|
|
|
ensure
|
2014-12-17 13:18:22 -05:00
|
|
|
Object.send :remove_const, :NotifierMailerPreview
|
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
|
2015-01-07 18:55:07 -05:00
|
|
|
assert_file "app/views/notifier_mailer/foo.text.erb" do |view|
|
|
|
|
assert_match(%r(\sapp/views/notifier_mailer/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
|
|
|
|
|
2015-01-07 18:55:07 -05:00
|
|
|
assert_file "app/views/notifier_mailer/bar.text.erb" do |view|
|
|
|
|
assert_match(%r(\sapp/views/notifier_mailer/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
|
2016-05-13 02:02:36 -04:00
|
|
|
|
|
|
|
assert_file "app/views/layouts/mailer.text.erb" do |view|
|
|
|
|
assert_match(/<%= yield %>/, view)
|
|
|
|
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
|
2015-01-07 18:55:07 -05:00
|
|
|
assert_file "app/views/notifier_mailer/foo.html.erb" do |view|
|
|
|
|
assert_match(%r(\sapp/views/notifier_mailer/foo\.html\.erb), view)
|
2013-12-19 19:12:55 -05:00
|
|
|
assert_match(/<%= @greeting %>/, view)
|
|
|
|
end
|
|
|
|
|
2015-01-07 18:55:07 -05:00
|
|
|
assert_file "app/views/notifier_mailer/bar.html.erb" do |view|
|
|
|
|
assert_match(%r(\sapp/views/notifier_mailer/bar\.html\.erb), view)
|
2013-12-19 19:12:55 -05:00
|
|
|
assert_match(/<%= @greeting %>/, view)
|
|
|
|
end
|
2016-05-13 02:02:36 -04:00
|
|
|
|
|
|
|
assert_file "app/views/layouts/mailer.html.erb" do |view|
|
|
|
|
assert_match(%r{<body>\n <%= yield %>\n </body>}, view)
|
|
|
|
end
|
2013-12-19 19:12:55 -05:00
|
|
|
end
|
|
|
|
|
2009-06-25 10:21:33 -04:00
|
|
|
def test_invokes_default_template_engine_even_with_no_action
|
|
|
|
run_generator ["notifier"]
|
2015-01-07 18:55:07 -05:00
|
|
|
assert_file "app/views/notifier_mailer"
|
2009-06-25 10:21:33 -04:00
|
|
|
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"]
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_file "app/mailers/farm/animal_mailer.rb" do |mailer|
|
|
|
|
assert_match(/class Farm::AnimalMailer < ApplicationMailer/, mailer)
|
2015-01-08 22:24:22 -05:00
|
|
|
assert_match(/en\.farm\.animal_mailer\.moos\.subject/, mailer)
|
2010-10-06 02:29:11 -04:00
|
|
|
end
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_file "test/mailers/previews/farm/animal_mailer_preview.rb" do |preview|
|
2015-03-14 19:57:06 -04:00
|
|
|
assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal_mailer/, preview)
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_match(/class Farm::AnimalMailerPreview < ActionMailer::Preview/, preview)
|
2015-03-14 19:57:06 -04:00
|
|
|
assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal_mailer\/moos/, preview)
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
2015-01-07 18:55:07 -05:00
|
|
|
assert_file "app/views/farm/animal_mailer/moos.text.erb"
|
|
|
|
assert_file "app/views/farm/animal_mailer/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
|
|
|
|
2014-12-17 13:18:22 -05:00
|
|
|
assert_file "app/mailers/notifier_mailer.rb" do |mailer|
|
2010-01-25 07:13:29 -05:00
|
|
|
assert_instance_method :foo, mailer do |foo|
|
2017-05-06 15:08:58 -04: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|
|
2017-05-06 15:08:58 -04: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
|
2014-11-27 00:36:10 -05:00
|
|
|
|
|
|
|
def test_mailer_on_revoke
|
|
|
|
run_generator
|
|
|
|
run_generator ["notifier"], behavior: :revoke
|
|
|
|
|
|
|
|
assert_no_file "app/mailers/notifier.rb"
|
|
|
|
assert_no_file "app/views/notifier/foo.text.erb"
|
|
|
|
assert_no_file "app/views/notifier/bar.text.erb"
|
|
|
|
assert_no_file "app/views/notifier/foo.html.erb"
|
|
|
|
assert_no_file "app/views/notifier/bar.html.erb"
|
|
|
|
end
|
2014-12-17 13:18:22 -05:00
|
|
|
|
|
|
|
def test_mailer_suffix_is_not_duplicated
|
|
|
|
run_generator ["notifier_mailer"]
|
|
|
|
|
|
|
|
assert_no_file "app/mailers/notifier_mailer_mailer.rb"
|
|
|
|
assert_file "app/mailers/notifier_mailer.rb"
|
2015-01-07 18:55:07 -05:00
|
|
|
|
|
|
|
assert_no_file "app/views/notifier_mailer_mailer/"
|
|
|
|
assert_file "app/views/notifier_mailer/"
|
|
|
|
|
|
|
|
assert_no_file "test/mailers/notifier_mailer_mailer_test.rb"
|
|
|
|
assert_file "test/mailers/notifier_mailer_test.rb"
|
|
|
|
|
|
|
|
assert_no_file "test/mailers/previews/notifier_mailer_mailer_preview.rb"
|
|
|
|
assert_file "test/mailers/previews/notifier_mailer_preview.rb"
|
2014-12-17 13:18:22 -05:00
|
|
|
end
|
2009-06-25 10:21:33 -04:00
|
|
|
end
|