Module: ActiveNotify::TestHelper
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/active_notify/testing/minitest.rb
Overview
Active Notify TestHelper
Provides assertions and helpers for testing notifiers. When included into a test case, deliveries made via Base#deliver_now and Base#deliver_later are captured instead of being dispatched to their carriers, so tests can assert on what was sent without triggering side effects.
class CommentNotifierTest < ActiveSupport::TestCase
include ActiveNotify::TestHelper
test "notifies on new comment" do
assert_notify_deliveries 1 do
CommentNotifier.with(user: user).deliver_now
end
end
end
Instance Method Summary collapse
-
#assert_enqueued_notify_deliveries(number, &block) ⇒ Object
Asserts that the number of notifications enqueued for background delivery (via
deliver_later) matches the given number. -
#assert_no_enqueued_notify_deliveries ⇒ Object
Asserts that no notifications were enqueued for background delivery (via
deliver_later). -
#assert_no_notify_deliveries ⇒ Object
Asserts that no notifications were delivered synchronously (via
deliver_now). -
#assert_notify_deliveries(number, &block) ⇒ Object
Asserts that the number of notifications delivered synchronously (via
deliver_now) matches the given number. -
#capture_notify_deliveries ⇒ Object
Returns an array of the notifications recorded inside the block.
Instance Method Details
#assert_enqueued_notify_deliveries(number, &block) ⇒ Object
Asserts that the number of notifications enqueued for background delivery (via deliver_later) matches the given number.
def test_notify_deliveries_enqueued
assert_enqueued_notify_deliveries 0
CommentNotifier.deliver_later
assert_enqueued_notify_deliveries 1
CommentNotifier.deliver_later
assert_enqueued_notify_deliveries 2
end
If a block is passed, that block should cause the specified number of notifications to be enqueued.
def test_notify_deliveries_enqueued_again
assert_enqueued_notify_deliveries 1 do
CommentNotifier.deliver_later
end
assert_enqueued_notify_deliveries 2 do
CommentNotifier.deliver_later
AnotherNotifier.deliver_later
end
end
100 101 102 |
# File 'lib/active_notify/testing/minitest.rb', line 100 def assert_enqueued_notify_deliveries(number, &block) _assert_notify_deliveries(number, verb: "enqueued", filter: ENQUEUED_FILTER, &block) end |
#assert_no_enqueued_notify_deliveries ⇒ Object
Asserts that no notifications were enqueued for background delivery (via deliver_later).
def test_notifications_not_enqueued
assert_no_enqueued_notify_deliveries
end
If a block is passed, that block should not cause any notifications to be enqueued.
def test_notifications_not_enqueued_in_block
assert_no_enqueued_notify_deliveries do
CommentNotifier.deliver_now
end
end
119 120 121 |
# File 'lib/active_notify/testing/minitest.rb', line 119 def assert_no_enqueued_notify_deliveries(&) assert_enqueued_notify_deliveries(0, &) end |
#assert_no_notify_deliveries ⇒ Object
Asserts that no notifications were delivered synchronously (via deliver_now).
def test_notifications_not_delivered
assert_no_notify_deliveries
end
If a block is passed, that block should not cause any notifications to be delivered.
def test_notifications_not_delivered_in_block
assert_no_notify_deliveries do
CommentNotifier.deliver_later
end
end
72 73 74 |
# File 'lib/active_notify/testing/minitest.rb', line 72 def assert_no_notify_deliveries(&) assert_notify_deliveries(0, &) end |
#assert_notify_deliveries(number, &block) ⇒ Object
Asserts that the number of notifications delivered synchronously (via deliver_now) matches the given number.
def test_notify_deliveries
assert_notify_deliveries 0
CommentNotifier.deliver_now
assert_notify_deliveries 1
CommentNotifier.deliver_now
assert_notify_deliveries 2
end
If a block is passed, that block should cause the specified number of notifications to be delivered.
def test_notify_deliveries_again
assert_notify_deliveries 1 do
CommentNotifier.deliver_now
end
assert_notify_deliveries 2 do
CommentNotifier.deliver_now
AnotherNotifier.deliver_now
end
end
53 54 55 |
# File 'lib/active_notify/testing/minitest.rb', line 53 def assert_notify_deliveries(number, &block) _assert_notify_deliveries(number, verb: "delivered", &block) end |
#capture_notify_deliveries ⇒ Object
Returns an array of the notifications recorded inside the block. Each entry is a hash with :notifier_class, :carrier_name, :method_name (:deliver_now or :deliver_later), :params, and :args.
def test_capture_notify_deliveries
deliveries = capture_notify_deliveries do
CommentNotifier.with(user: user).deliver_now
end
assert_equal 1, deliveries.size
assert_equal :deliver_now, deliveries.first[:method_name]
assert_equal({ user: user }, deliveries.first[:params])
end
137 138 139 140 141 142 143 |
# File 'lib/active_notify/testing/minitest.rb', line 137 def capture_notify_deliveries raise ArgumentError, "block is required" unless block_given? original_count = TestDelivery.deliveries.size yield TestDelivery.deliveries[original_count..] || [] end |