Class: AbstractNotifier::HaveSentNotification

Inherits:
RSpec::Matchers::BuiltIn::BaseMatcher
  • Object
show all
Defined in:
lib/abstract_notifier/testing/rspec.rb

Direct Known Subclasses

HaveEnqueuedNotification

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload = nil) ⇒ HaveSentNotification

Returns a new instance of HaveSentNotification.



7
8
9
10
# File 'lib/abstract_notifier/testing/rspec.rb', line 7

def initialize(payload = nil)
  @payload = payload
  set_expected_number(:exactly, 1)
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/abstract_notifier/testing/rspec.rb', line 5

def payload
  @payload
end

Instance Method Details

#at_least(count) ⇒ Object



17
18
19
20
# File 'lib/abstract_notifier/testing/rspec.rb', line 17

def at_least(count)
  set_expected_number(:at_least, count)
  self
end

#at_most(count) ⇒ Object



22
23
24
25
# File 'lib/abstract_notifier/testing/rspec.rb', line 22

def at_most(count)
  set_expected_number(:at_most, count)
  self
end

#deliveriesObject



77
78
79
# File 'lib/abstract_notifier/testing/rspec.rb', line 77

def deliveries
  AbstractNotifier::Testing::Driver.deliveries
end

#exactly(count) ⇒ Object



12
13
14
15
# File 'lib/abstract_notifier/testing/rspec.rb', line 12

def exactly(count)
  set_expected_number(:exactly, count)
  self
end

#failure_messageObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/abstract_notifier/testing/rspec.rb', line 92

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_negatedObject



107
108
109
# File 'lib/abstract_notifier/testing/rspec.rb', line 107

def failure_message_when_negated
  "expected not to #{verb_present} #{payload}"
end

#matches?(proc) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


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
# 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|
      next true if payload.nil?

      if payload.is_a?(::Hash) && !payload[:via]
        actual_payload = actual_payload.dup
        actual_payload.delete(:via)
      end

      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

#message_expectation_modifierObject



111
112
113
114
115
116
117
118
# File 'lib/abstract_notifier/testing/rspec.rb', line 111

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

#onceObject



31
32
33
# File 'lib/abstract_notifier/testing/rspec.rb', line 31

def once
  exactly(:once)
end

#payload_descriptionObject



120
121
122
123
124
125
126
# File 'lib/abstract_notifier/testing/rspec.rb', line 120

def payload_description
  if payload.is_a?(RSpec::Matchers::Composable)
    payload.description
  else
    payload
  end
end

#set_expected_number(relativity, count) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/abstract_notifier/testing/rspec.rb', line 81

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

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/abstract_notifier/testing/rspec.rb', line 43

def supports_block_expectations?
  true
end

#thriceObject



39
40
41
# File 'lib/abstract_notifier/testing/rspec.rb', line 39

def thrice
  exactly(:thrice)
end

#timesObject



27
28
29
# File 'lib/abstract_notifier/testing/rspec.rb', line 27

def times
  self
end

#twiceObject



35
36
37
# File 'lib/abstract_notifier/testing/rspec.rb', line 35

def twice
  exactly(:twice)
end

#verb_pastObject



128
129
130
# File 'lib/abstract_notifier/testing/rspec.rb', line 128

def verb_past
  "sent"
end

#verb_presentObject



132
133
134
# File 'lib/abstract_notifier/testing/rspec.rb', line 132

def verb_present
  "send"
end