class AbstractNotifier::HaveSentNotification

Attributes

payload[R]

Public Class Methods

new(payload = nil) click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 7
def initialize(payload = nil)
  @payload = payload
  set_expected_number(:exactly, 1)
end

Public Instance Methods

at_least(count) click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 17
def at_least(count)
  set_expected_number(:at_least, count)
  self
end
at_most(count) click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 22
def at_most(count)
  set_expected_number(:at_most, count)
  self
end
exactly(count) click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 12
def exactly(count)
  set_expected_number(:exactly, count)
  self
end
matches?(proc) click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 47
def matches?(proc)
  raise ArgumentError, "have_sent_notification only supports block expectations" unless Proc === proc

  raise "You can only use have_sent_notification matcher in :test delivery mode" unless AbstractNotifier.test?

  original_deliveries_count = deliveries.count
  proc.call
  in_block_deliveries = deliveries.drop(original_deliveries_count)

  @matching_deliveries, @unmatching_deliveries =
    in_block_deliveries.partition do |actual_payload|
      payload.nil? || (payload === actual_payload)
    end

  @matching_count = @matching_deliveries.size

  case @expectation_type
  when :exactly then @expected_number == @matching_count
  when :at_most then @expected_number >= @matching_count
  when :at_least then @expected_number <= @matching_count
  end
end
once() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 31
def once
  exactly(:once)
end
supports_block_expectations?() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 43
def supports_block_expectations?
  true
end
thrice() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 39
def thrice
  exactly(:thrice)
end
times() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 27
def times
  self
end
twice() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 35
def twice
  exactly(:twice)
end

Private Instance Methods

deliveries() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 72
def deliveries
  AbstractNotifier::Testing::Driver.deliveries
end
failure_message() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 87
def failure_message
  (+"expected to #{verb_present} notification: #{payload_description}").tap do |msg|
    msg << " #{message_expectation_modifier}, but"

    if @unmatching_deliveries.any?
      msg << " #{verb_past} the following notifications:"
      @unmatching_deliveries.each do |unmatching_payload|
        msg << "\n  #{unmatching_payload}"
      end
    else
      msg << " haven't #{verb_past} anything"
    end
  end
end
failure_message_when_negated() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 102
def failure_message_when_negated
  "expected not to #{verb_present} #{payload}"
end
message_expectation_modifier() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 106
def message_expectation_modifier
  number_modifier = @expected_number == 1 ? "once" : "#{@expected_number} times"
  case @expectation_type
  when :exactly then "exactly #{number_modifier}"
  when :at_most then "at most #{number_modifier}"
  when :at_least then "at least #{number_modifier}"
  end
end
payload_description() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 115
def payload_description
  if payload.is_a?(RSpec::Matchers::Composable)
    payload.description
  else
    payload
  end
end
set_expected_number(relativity, count) click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 76
def set_expected_number(relativity, count)
  @expectation_type = relativity
  @expected_number =
    case count
    when :once then 1
    when :twice then 2
    when :thrice then 3
    else Integer(count)
    end
end
verb_past() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 123
def verb_past
  "sent"
end
verb_present() click to toggle source
# File lib/abstract_notifier/testing/rspec.rb, line 127
def verb_present
  "send"
end