Class: Downstream::HavePublishedEvent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_class) ⇒ HavePublishedEvent

Returns a new instance of HavePublishedEvent.



7
8
9
10
# File 'lib/downstream/rspec/have_published_event.rb', line 7

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/downstream/rspec/have_published_event.rb', line 5

def attributes
  @attributes
end

#event_classObject (readonly)

Returns the value of attribute event_class.



5
6
7
# File 'lib/downstream/rspec/have_published_event.rb', line 5

def event_class
  @event_class
end

Instance Method Details

#at_least(count) ⇒ Object



22
23
24
25
# File 'lib/downstream/rspec/have_published_event.rb', line 22

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

#at_most(count) ⇒ Object



27
28
29
30
# File 'lib/downstream/rspec/have_published_event.rb', line 27

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

#exactly(count) ⇒ Object



17
18
19
20
# File 'lib/downstream/rspec/have_published_event.rb', line 17

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

#failure_messageObject



76
77
78
79
80
# File 'lib/downstream/rspec/have_published_event.rb', line 76

def failure_message
  "expected to publish #{event_class.identifier} event".tap do |msg|
    msg << " #{message_expectation_modifier}, but haven't published"
  end
end

#failure_message_when_negatedObject



82
83
84
# File 'lib/downstream/rspec/have_published_event.rb', line 82

def failure_message_when_negated
  "expected not to publish #{event_class.identifier} event"
end

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/downstream/rspec/have_published_event.rb', line 52

def matches?(block)
  raise ArgumentError, "have_published_event only supports block expectations" unless block.is_a?(Proc)

  @matching_events = []

  subscriber = ->(event) do
    if attributes.nil? || attributes_match?(event)
      @matching_events << event
    end
  end

  Downstream.subscribed(subscriber, to: event_class) do
    block.call
  end

  @matching_count = @matching_events.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



36
37
38
# File 'lib/downstream/rspec/have_published_event.rb', line 36

def once
  exactly(:once)
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/downstream/rspec/have_published_event.rb', line 48

def supports_block_expectations?
  true
end

#thriceObject



44
45
46
# File 'lib/downstream/rspec/have_published_event.rb', line 44

def thrice
  exactly(:thrice)
end

#timesObject



32
33
34
# File 'lib/downstream/rspec/have_published_event.rb', line 32

def times
  self
end

#twiceObject



40
41
42
# File 'lib/downstream/rspec/have_published_event.rb', line 40

def twice
  exactly(:twice)
end

#with(attributes) ⇒ Object



12
13
14
15
# File 'lib/downstream/rspec/have_published_event.rb', line 12

def with(attributes)
  @attributes = attributes
  self
end