Skip to content

Commit 9499632

Browse files
authored
Merge pull request rails#53818 from larouxn/notification_assertions_actionmailer
Migrate applicable `actionmailer` tests to use NotificationAssertions
2 parents a095771 + cfb9fee commit 9499632

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

actionmailer/test/base_test.rb

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -963,31 +963,24 @@ def a_callback
963963
end
964964

965965
test "notification for process" do
966-
events = []
967-
ActiveSupport::Notifications.subscribe("process.action_mailer") { |event| events << event }
966+
expected_payload = { mailer: "BaseMailer", action: :welcome, args: [{ body: "Hello there" }] }
968967

969-
BaseMailer.welcome(body: "Hello there").deliver_now
970-
971-
assert_equal 1, events.length
972-
assert_equal "process.action_mailer", events[0].name
973-
assert_equal "BaseMailer", events[0].payload[:mailer]
974-
assert_equal :welcome, events[0].payload[:action]
975-
assert_equal [{ body: "Hello there" }], events[0].payload[:args]
976-
ensure
977-
ActiveSupport::Notifications.unsubscribe "process.action_mailer"
968+
assert_notifications_count("process.action_mailer", 1) do
969+
assert_notification("process.action_mailer", expected_payload) do
970+
BaseMailer.welcome(body: "Hello there").deliver_now
971+
end
972+
end
978973
end
979974

980975
test "notification for deliver" do
981-
events = []
982-
ActiveSupport::Notifications.subscribe("deliver.action_mailer") { |event| events << event }
983-
984-
BaseMailer.welcome(body: "Hello there").deliver_now
976+
event = capture_notifications("deliver.action_mailer") do
977+
assert_notifications_count("deliver.action_mailer", 1) do
978+
BaseMailer.welcome(body: "Hello there").deliver_now
979+
end
980+
end.first
985981

986-
assert_equal 1, events.length
987-
assert_equal "deliver.action_mailer", events[0].name
988-
assert_not_nil events[0].payload[:message_id]
989-
ensure
990-
ActiveSupport::Notifications.unsubscribe "deliver.action_mailer"
982+
assert_equal "deliver.action_mailer", event.name
983+
assert_not_nil event.payload[:message_id]
991984
end
992985

993986
private

actionmailer/test/caching_test.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,15 @@ def test_multipart_fragment_caching
171171

172172
def test_fragment_cache_instrumentation
173173
@mailer.enable_fragment_cache_logging = true
174-
payload = nil
175174

176-
subscriber = proc do |_, _, _, _, event_payload|
177-
payload = event_payload
178-
end
175+
expected_payload = {
176+
mailer: "caching_mailer",
177+
key: [:views, "caching_mailer/fragment_cache:#{template_digest("caching_mailer/fragment_cache", "html")}", :caching]
178+
}
179179

180-
ActiveSupport::Notifications.subscribed(subscriber, "read_fragment.action_mailer") do
180+
assert_notification("read_fragment.action_mailer", expected_payload) do
181181
@mailer.fragment_cache
182182
end
183-
184-
assert_equal "caching_mailer", payload[:mailer]
185-
assert_equal [ :views, "caching_mailer/fragment_cache:#{template_digest("caching_mailer/fragment_cache", "html")}", :caching ], payload[:key]
186183
ensure
187184
@mailer.enable_fragment_cache_logging = true
188185
end

0 commit comments

Comments
 (0)