1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionmailer/test/parameterized_test.rb
Luke Pearce c90c6c1344 Parameterized mailers can configure delivery job
Setting parameterized_delivery_job on a mailer class will cause Parameterized::MessageDelivery to use
the specified job instead of ActionMailer::Parameterized::DeliveryJob:

    class MyMailer < ApplicationMailer
      self.parameterized_delivery_job = MyCustomDeliveryJob
      ...
    end
2018-10-05 17:02:40 +01:00

69 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require "abstract_unit"
require "active_job"
require "mailers/params_mailer"
class ParameterizedTest < ActiveSupport::TestCase
include ActiveJob::TestHelper
setup do
@previous_logger = ActiveJob::Base.logger
ActiveJob::Base.logger = Logger.new(nil)
@previous_delivery_method = ActionMailer::Base.delivery_method
ActionMailer::Base.delivery_method = :test
@previous_deliver_later_queue_name = ActionMailer::Base.deliver_later_queue_name
ActionMailer::Base.deliver_later_queue_name = :test_queue
@mail = ParamsMailer.with(inviter: "david@basecamp.com", invitee: "jason@basecamp.com").invitation
end
teardown do
ActiveJob::Base.logger = @previous_logger
ParamsMailer.deliveries.clear
ActionMailer::Base.delivery_method = @previous_delivery_method
ActionMailer::Base.deliver_later_queue_name = @previous_deliver_later_queue_name
end
test "parameterized headers" do
assert_equal(["jason@basecamp.com"], @mail.to)
assert_equal(["david@basecamp.com"], @mail.from)
assert_equal("So says david@basecamp.com", @mail.body.encoded)
end
test "enqueue the email with params" do
assert_performed_with(job: ActionMailer::Parameterized::DeliveryJob, args: ["ParamsMailer", "invitation", "deliver_now", { inviter: "david@basecamp.com", invitee: "jason@basecamp.com" } ]) do
@mail.deliver_later
end
end
test "respond_to?" do
mailer = ParamsMailer.with(inviter: "david@basecamp.com", invitee: "jason@basecamp.com")
assert_respond_to mailer, :invitation
assert_not_respond_to mailer, :anything
invitation = mailer.method(:invitation)
assert_equal Method, invitation.class
assert_raises(NameError) do
invitation = mailer.method(:anything)
end
end
test "should enqueue a parameterized request with the correct delivery job" do
old_delivery_job = ParamsMailer.parameterized_delivery_job
ParamsMailer.parameterized_delivery_job = ParameterizedDummyJob
assert_performed_with(job: ParameterizedDummyJob, args: ["ParamsMailer", "invitation", "deliver_now", { inviter: "david@basecamp.com", invitee: "jason@basecamp.com" } ]) do
@mail.deliver_later
end
ParamsMailer.parameterized_delivery_job = old_delivery_job
end
class ParameterizedDummyJob < ActionMailer::Parameterized::DeliveryJob; end
end