class ActiveEventStore::HavePublishedEvent

Attributes

attributes[R]
event_class[R]
event_store[R]

Public Class Methods

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

Public Instance Methods

at_least(count) click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 28
def at_least(count)
  set_expected_number(:at_least, count)
  self
end
at_most(count) click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 33
def at_most(count)
  set_expected_number(:at_most, count)
  self
end
exactly(count) click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 23
def exactly(count)
  set_expected_number(:exactly, count)
  self
end
matches?(block) click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 58
def matches?(block)
  raise ArgumentError, "have_published_event only supports block expectations" unless block.is_a?(Proc)

  original_count = event_store.read.count
  block.call
  new_count = event_store.read.count - original_count
  in_block_events = new_count.positive? ? event_store.read.backward.limit(new_count).to_a :
                                          []

  @matching_events, @unmatching_events =
    in_block_events.partition do |actual_event|
      (event_class.identifier == actual_event.event_type) &&
        (attributes.nil? || attributes_match?(actual_event))
    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/active_event_store/rspec/have_published_event.rb, line 42
def once
  exactly(:once)
end
supports_block_expectations?() click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 54
def supports_block_expectations?
  true
end
thrice() click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 50
def thrice
  exactly(:thrice)
end
times() click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 38
def times
  self
end
twice() click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 46
def twice
  exactly(:twice)
end
with(attributes) click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 18
def with(attributes)
  @attributes = attributes
  self
end
with_store(store) click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 13
def with_store(store)
  @event_store = store
  self
end

Private Instance Methods

attributes_match?(event) click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 84
def attributes_match?(event)
  RSpec::Matchers::BuiltIn::HaveAttributes.new(attributes).matches?(event)
end
failure_message() click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 99
def failure_message
  (+"expected to publish #{event_class.identifier} event").tap do |msg|
    msg << " #{message_expectation_modifier}, but"

    if @unmatching_events.any?
      msg << " published the following events:"
      @unmatching_events.each do |unmatching_event|
        msg << "\n  #{unmatching_event.inspect}"
      end
    else
      msg << " haven't published anything"
    end
  end
end
failure_message_when_negated() click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 114
def failure_message_when_negated
  "expected not to publish #{event_class.identifier} event"
end
message_expectation_modifier() click to toggle source
# File lib/active_event_store/rspec/have_published_event.rb, line 118
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/active_event_store/rspec/have_published_event.rb, line 88
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