aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/test/parameterized_test.rb
blob: 3713dc3e3e4dafe2d245e9b7a51082b6cf5845b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

require "abstract_unit"
require "active_job"
require "mailers/params_mailer"

class ParameterizedTest < ActiveSupport::TestCase
  include ActiveJob::TestHelper

  class DummyDeliveryJob < ActionMailer::DeliveryJob
  end

  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
    args = [
      "ParamsMailer",
      "invitation",
      "deliver_now",
      { inviter: "david@basecamp.com", invitee: "jason@basecamp.com" },
    ]
    assert_performed_with(job: ActionMailer::DeliveryJob, args: args) 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
    args = [
      "ParamsMailer",
      "invitation",
      "deliver_now",
      { inviter: "david@basecamp.com", invitee: "jason@basecamp.com" },
    ]

    with_delivery_job DummyDeliveryJob do
      assert_performed_with(job: DummyDeliveryJob, args: args) do
        @mail.deliver_later
      end
    end
  end

  private

    def with_delivery_job(job)
      old_delivery_job = ParamsMailer.delivery_job
      ParamsMailer.delivery_job = job
    ensure
      ParamsMailer.delivery_job = old_delivery_job
    end
end