Class: ActiveEventStore::TestHelper::EventPublishedMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/active_event_store/test_helper/event_published_matcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected_event_class, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, refute: false) ⇒ EventPublishedMatcher

Returns a new instance of EventPublishedMatcher.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 9

def initialize(expected_event_class, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, refute: false)
  @event_class = expected_event_class
  @store = store || ActiveEventStore.event_store
  @attributes = with
  @refute = refute

  count_expectations = {
    exactly: exactly,
    at_most: at_most,
    at_least: at_least
  }.reject { |_, v| v.nil? }

  if count_expectations.length > 1
    raise ArgumentError("Only one of :exactly, :at_least or :at_most can be specified")
  elsif count_expectations.length == 0
    @count_expectation_kind = :at_least
    @expected_count = 1
  else
    @count_expectation_kind = count_expectations.keys.first
    @expected_count = count_expectations.values.first
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 6

def attributes
  @attributes
end

#matching_eventsObject (readonly)

Returns the value of attribute matching_events.



6
7
8
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 6

def matching_events
  @matching_events
end

Class Method Details

.event_data_matches?(attributes, event) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 144

def event_data_matches?(attributes, event)
  (attributes.nil? || attributes.all? { |k, v| v == event.public_send(k) })
end

.event_matches?(event_class, attributes, event) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 136

def event_matches?(event_class, attributes, event)
  event_type_matches?(event_class, event) && event_data_matches?(attributes, event)
end

.event_type_matches?(event_class, event) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 140

def event_type_matches?(event_class, event)
  event_class.identifier == event.event_type
end

Instance Method Details

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
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
76
77
78
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 38

def matches?(block)
  raise ArgumentError, "#{assertion_name} only support block assertions" if block.nil?

  events = with_published_events do
    block.call
  end

  @matching_events, @unmatching_events = partition_events(events)

  mismatch_message = count_mismatch_message(@matching_events.size)

  if mismatch_message
    expectations = [
      "Expected #{mismatch_message} #{@event_class.identifier}"
    ]

    expectations << if refute?
      report_events = @matching_events
      "not to have been published"
    else
      report_events = @unmatching_events
      "to have been published"
    end

    expectations << "with attributes #{attributes.inspect}" unless attributes.nil?

    expectations << expectations.pop + ", but"

    expectations << if report_events.any?
      report_events.inject("published the following events instead:") do |msg, event|
        msg + "\n  #{event.inspect}"
      end
    else
      "hasn't published anything"
    end

    return expectations.join(" ")
  end

  nil
end

#with_published_events(&block) ⇒ Object



32
33
34
35
36
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 32

def with_published_events(&block)
  original_count = @store.read.count
  block.call
  in_block_events(original_count, @store.read.count)
end