Class: AbstractNotifier::HaveSentNotification
- Inherits:
-
RSpec::Matchers::BuiltIn::BaseMatcher
- Object
- RSpec::Matchers::BuiltIn::BaseMatcher
- AbstractNotifier::HaveSentNotification
- Defined in:
- lib/abstract_notifier/testing/rspec.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
Instance Method Summary collapse
- #at_least(count) ⇒ Object
- #at_most(count) ⇒ Object
- #deliveries ⇒ Object
- #exactly(count) ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(payload = nil) ⇒ HaveSentNotification
constructor
A new instance of HaveSentNotification.
- #matches?(proc) ⇒ Boolean
- #message_expectation_modifier ⇒ Object
- #once ⇒ Object
- #payload_description ⇒ Object
- #set_expected_number(relativity, count) ⇒ Object
- #supports_block_expectations? ⇒ Boolean
- #thrice ⇒ Object
- #times ⇒ Object
- #twice ⇒ Object
- #verb_past ⇒ Object
- #verb_present ⇒ Object
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
#payload ⇒ Object (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 |
#deliveries ⇒ Object
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_message ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/abstract_notifier/testing/rspec.rb', line 92 def (+"expected to #{verb_present} notification: #{payload_description}").tap do |msg| msg << " #{}, 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 ⇒ Object
107 108 109 |
# File 'lib/abstract_notifier/testing/rspec.rb', line 107 def "expected not to #{verb_present} #{payload}" end |
#matches?(proc) ⇒ Boolean
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_modifier ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/abstract_notifier/testing/rspec.rb', line 111 def 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 |
#once ⇒ Object
31 32 33 |
# File 'lib/abstract_notifier/testing/rspec.rb', line 31 def once exactly(:once) end |
#payload_description ⇒ Object
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
43 44 45 |
# File 'lib/abstract_notifier/testing/rspec.rb', line 43 def supports_block_expectations? true end |
#thrice ⇒ Object
39 40 41 |
# File 'lib/abstract_notifier/testing/rspec.rb', line 39 def thrice exactly(:thrice) end |
#times ⇒ Object
27 28 29 |
# File 'lib/abstract_notifier/testing/rspec.rb', line 27 def times self end |
#twice ⇒ Object
35 36 37 |
# File 'lib/abstract_notifier/testing/rspec.rb', line 35 def twice exactly(:twice) end |
#verb_past ⇒ Object
128 129 130 |
# File 'lib/abstract_notifier/testing/rspec.rb', line 128 def verb_past "sent" end |
#verb_present ⇒ Object
132 133 134 |
# File 'lib/abstract_notifier/testing/rspec.rb', line 132 def verb_present "send" end |