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

#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

#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
# 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

#onceObject



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

def once
  exactly(:once)
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