Skip to content

Commit

Permalink
Fix refute implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscz committed Jan 27, 2021
1 parent fcf3269 commit e745515
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/active_event_store/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,26 @@ def matches?(block)

mismatch_message = count_mismatch_message(@matching_events.size)

unless mismatch_message.nil?
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 @unmatching_events.any?
@unmatching_events.inject("published the following events instead:") do |msg, unmatching_event|
msg + "\n #{unmatching_event.inspect}"
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"
Expand Down Expand Up @@ -109,8 +111,8 @@ def in_block_events(before_block_count, after_block_count)
# Partitions events into matching and unmatching
def partition_events(events)
events.partition do |actual_event|
negate_on_refute((@event_class.identifier == actual_event.event_type) &&
(@attributes.nil? || @attributes.all? { |k, v| v == actual_event.public_send(k) }))
(@event_class.identifier == actual_event.event_type) &&
(@attributes.nil? || @attributes.all? { |k, v| v == actual_event.public_send(k) })
end
end

Expand Down Expand Up @@ -153,6 +155,8 @@ def assert_event_published(expected_event, store: nil, with: nil, exactly: nil,
matcher.matching_events
end

# Asserts that the given event was *not* published `exactly`, `at_least` or `at_most` number of times
# to a specific `store` `with` a particular hash of attributes.
def refute_event_published(expected_event, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, &block)
matcher = EventPublishedMatcher.new(
expected_event,
Expand Down
22 changes: 22 additions & 0 deletions test/active_event_store/test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,26 @@ def test_assert_event_published_with_attribute_mismatch

assert_match(/at least 1 test_event.*with attributes.*:user_id=>25.*:action_type=>"death".*but published(.|[\n])*:user_id=>25.*:action_type=>"birth"/s, e.message)
end

# == Refute tests
def test_refute_event_published_with_published_event
e = assert_raises do
refute_event_published(ActiveEventStore::TestEvent, with: {user_id: 25, action_type: "birth"}) do
ActiveEventStore.publish(event)
end
end

assert_match(/at least 1 test_event not.*with attributes.*:user_id=>25.*:action_type=>"birth".*/s, e.message)
end

def test_refute_event_published_with_attribute_mismatched
refute_event_published(ActiveEventStore::TestEvent, with: {user_id: 25, action_type: "death"}) do
ActiveEventStore.publish(event)
end
end

def test_refute_event_published_on_no_events
refute_event_published(ActiveEventStore::TestEvent, with: {user_id: 25, action_type: "death"}) do
end
end
end

0 comments on commit e745515

Please sign in to comment.