diff --git a/Rakefile b/Rakefile index 5c8d53c..7bba35b 100644 --- a/Rakefile +++ b/Rakefile @@ -15,5 +15,4 @@ Rake::TestTask.new(:test) do |t| t.warning = false end - task default: [:rubocop, :spec] diff --git a/lib/active_event_store/test_helper.rb b/lib/active_event_store/test_helper.rb index 2cd70a6..328a5de 100644 --- a/lib/active_event_store/test_helper.rb +++ b/lib/active_event_store/test_helper.rb @@ -2,31 +2,30 @@ module ActiveEventStore module TestHelper - class EventPublishedMatcher attr_reader :attributes, - :matching_events + :matching_events 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 + @store = store || ActiveEventStore.event_store + @attributes = with + @refute = refute count_expectations = { - exactly: exactly, - at_most: at_most, - at_least: at_least, + 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 + @expected_count = 1 else @count_expectation_kind = count_expectations.keys.first - @expected_count = count_expectations.values.first + @expected_count = count_expectations.values.first end end @@ -60,7 +59,7 @@ def matches?(block) expectations << "with attributes #{attributes.inspect}" unless attributes.nil? - expectations << expectations.pop + ', but' + expectations << expectations.pop + ", but" expectations << if @unmatching_events.any? @unmatching_events.inject("published the following events instead:") do |msg, unmatching_event| @@ -70,7 +69,7 @@ def matches?(block) "hasn't published anything" end - return expectations.join(' ') + return expectations.join(" ") end nil @@ -133,7 +132,6 @@ def count_mismatch_message(actual_count) raise ArgumentError, "Unrecognized expectation kind: #{@count_expectation_kind}" end end - end # Asserts that the given event was published `exactly`, `at_least` or `at_most` number of times @@ -141,29 +139,29 @@ def count_mismatch_message(actual_count) def assert_event_published(expected_event, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, &block) matcher = EventPublishedMatcher.new( expected_event, - store: store, - with: with, - exactly: exactly, + store: store, + with: with, + exactly: exactly, at_least: at_least, - at_most: at_most, + at_most: at_most ) if (msg = matcher.matches?(block)) fail(msg) end - return matcher.matching_events + matcher.matching_events end def refute_event_published(expected_event, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, &block) matcher = EventPublishedMatcher.new( expected_event, - store: store, - with: with, - exactly: exactly, + store: store, + with: with, + exactly: exactly, at_least: at_least, - at_most: at_most, - refute: true + at_most: at_most, + refute: true ) if (msg = matcher.matches?(block)) diff --git a/test/active_event_store/test_helper_test.rb b/test/active_event_store/test_helper_test.rb index 6b3c806..4e3ea34 100644 --- a/test/active_event_store/test_helper_test.rb +++ b/test/active_event_store/test_helper_test.rb @@ -1,18 +1,20 @@ -require 'test_helper' -require 'active_event_store/test_helper' -require_relative '../../spec/support/test_events' +# frozen_string_literal: true + +require "test_helper" +require "active_event_store/test_helper" +require_relative "../../spec/support/test_events" class TestHelperTest < Minitest::Test include ActiveEventStore::TestHelper attr_reader :event_class, - :event, - :event2 + :event, + :event2 def setup super @event_class = ActiveEventStore::TestEvent - @event = event_class.new(user_id: 25, action_type: "birth") - @event2 = event_class.new(user_id: 1, action_type: "death") + @event = event_class.new(user_id: 25, action_type: "birth") + @event2 = event_class.new(user_id: 1, action_type: "death") end def test_assert_event_published @@ -22,17 +24,16 @@ def test_assert_event_published assert events.length == 1 assert_includes events, event - end def test_assert_event_published_with_one_attribute - assert_event_published(ActiveEventStore::TestEvent, with: { user_id: 25 }) do + assert_event_published(ActiveEventStore::TestEvent, with: {user_id: 25}) do ActiveEventStore.publish(event) end end def test_assert_event_published_with_multiple_attributes - assert_event_published(ActiveEventStore::TestEvent, with: { user_id: 25, action_type: "birth" }) do + assert_event_published(ActiveEventStore::TestEvent, with: {user_id: 25, action_type: "birth"}) do ActiveEventStore.publish(event) end end @@ -47,13 +48,12 @@ def test_assert_event_published_with_count # == Failure cases def test_assert_event_published_with_no_events - e = assert_raises do assert_event_published(ActiveEventStore::TestEvent, exactly: 1) do end end - assert_match /exactly 1 test_event to have been published, but hasn't published anything/, e.message + assert_match(/exactly 1 test_event to have been published, but hasn't published anything/, e.message) end def test_assert_event_published_with_class_mismatch @@ -63,16 +63,16 @@ def test_assert_event_published_with_class_mismatch end end - assert_match /at least 1 active_event_store.another_test_event to have been published.*published the following events instead/, e.message + assert_match(/at least 1 active_event_store.another_test_event to have been published.*published the following events instead/, e.message) end def test_assert_event_published_with_attribute_mismatch e = assert_raises do - assert_event_published(ActiveEventStore::TestEvent, with: { user_id: 25, action_type: "death" }) do + assert_event_published(ActiveEventStore::TestEvent, with: {user_id: 25, action_type: "death"}) do ActiveEventStore.publish(event) end end - 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 + 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 end diff --git a/test/test_helper.rb b/test/test_helper.rb index f57b767..bbb703e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,7 +9,7 @@ ENV["RAILS_ENV"] = "test" -require 'bundler' +require "bundler" Bundler.require :default, :test begin