Module: Notify::TestHelper

Defined in:
lib/notify/test_helper.rb

Instance Method Summary collapse

Instance Method Details

#assert_notify_dispatched(name, count: nil, to: nil) ⇒ Object



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
# File 'lib/notify/test_helper.rb', line 5

def assert_notify_dispatched(name, count: nil, to: nil)
  name = name.to_sym
  matching = Notify.deliveries.select { |d| d[:name] == name }

  if matching.empty?
    delivered = Notify.deliveries.map { |d| d[:name] }.inspect
    raise "Expected Notify.message(:#{name}) to have been dispatched, but it was not. " \
          "Deliveries: #{delivered}"
  end

  if count && matching.size != count
    raise "Expected #{count} dispatch(es) of :#{name}, got #{matching.size}"
  end

  if to
    expected = Array(to)
    all_recipients = matching.flat_map do |d|
      (d[:resolved][:recipients] || {}).values.flatten
    end

    missing = expected - all_recipients
    unless missing.empty?
      raise "Expected recipients #{expected.inspect} for :#{name}, " \
            "but #{missing.inspect} not found in resolved recipients: #{all_recipients.inspect}"
    end
  end

  true
end

#last_notify_deliveryObject



35
36
37
# File 'lib/notify/test_helper.rb', line 35

def last_notify_delivery
  Notify.deliveries.last
end

#setup_notify_test_modeObject



39
40
41
42
# File 'lib/notify/test_helper.rb', line 39

def setup_notify_test_mode
  Notify.test_mode!
  Notify.deliveries.clear
end