class Downstream::HavePublishedEvent

Attributes

attributes[R]
event_class[R]

Public Class Methods

new(event_class) click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 7
def initialize(event_class)
  @event_class = event_class
  set_expected_number(:exactly, 1)
end

Public Instance Methods

at_least(count) click to toggle source
# 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) click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 27
def at_most(count)
  set_expected_number(:at_most, count)
  self
end
exactly(count) click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 17
def exactly(count)
  set_expected_number(:exactly, count)
  self
end
failure_message() click to toggle source
# 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_negated() click to toggle source
# 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) click to toggle source
# 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
once() click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 36
def once
  exactly(:once)
end
supports_block_expectations?() click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 48
def supports_block_expectations?
  true
end
thrice() click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 44
def thrice
  exactly(:thrice)
end
times() click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 32
def times
  self
end
twice() click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 40
def twice
  exactly(:twice)
end
with(attributes) click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 12
def with(attributes)
  @attributes = attributes
  self
end

Private Instance Methods

attributes_match?(event) click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 88
def attributes_match?(event)
  RSpec::Matchers::BuiltIn::HaveAttributes.new(attributes).matches?(event)
end
message_expectation_modifier() click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 103
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
set_expected_number(relativity, count) click to toggle source
# File lib/downstream/rspec/have_published_event.rb, line 92
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