diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-22 16:39:34 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-22 16:39:34 +0300 |
commit | e23c409e66b47a970a3cb83ac15d2ff906e75ce0 (patch) | |
tree | b1d580cd64c5d67a81a9445da42e82ceeefa96c5 /spec/support/matchers | |
parent | 2fa173410ad24b37aba6450ae4530ec231844d86 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support/matchers')
-rw-r--r-- | spec/support/matchers/event_store.rb | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/spec/support/matchers/event_store.rb b/spec/support/matchers/event_store.rb index 7cf25468eb2..582ea202187 100644 --- a/spec/support/matchers/event_store.rb +++ b/spec/support/matchers/event_store.rb @@ -6,7 +6,7 @@ RSpec::Matchers.define :publish_event do |expected_event_class| supports_block_expectations match do |proc| - raise ArgumentError, 'This matcher only supports block expectation' unless proc.respond_to?(:call) + raise ArgumentError, 'publish_event matcher only supports block expectation' unless proc.respond_to?(:call) @events ||= [] @@ -22,6 +22,8 @@ RSpec::Matchers.define :publish_event do |expected_event_class| end def match_data?(actual, expected) + return if actual.blank? || expected.blank? + values_match?(actual.keys, expected.keys) && actual.keys.all? do |key| values_match?(expected[key], actual[key]) @@ -33,7 +35,7 @@ RSpec::Matchers.define :publish_event do |expected_event_class| end failure_message do - message = "expected #{expected_event_class} with #{@expected_data} to be published" + message = "expected #{expected_event_class} with #{@expected_data || 'no data'} to be published" if @events.present? <<~MESSAGE @@ -46,7 +48,7 @@ RSpec::Matchers.define :publish_event do |expected_event_class| end match_when_negated do |proc| - raise ArgumentError, 'This matcher only supports block expectation' unless proc.respond_to?(:call) + raise ArgumentError, 'publish_event matcher only supports block expectation' unless proc.respond_to?(:call) allow(Gitlab::EventStore).to receive(:publish) @@ -57,7 +59,45 @@ RSpec::Matchers.define :publish_event do |expected_event_class| def events_list @events.map do |event| - " - #{event.class.name} #{event.data}" + " - #{event.class.name} with #{event.data}" end.join("\n") end end + +# not_publish_event enables multiple assertions on a single block, for example: +# expect { Model.create(invalid: :attribute) } +# .to not_change(Model, :count) +# .and not_publish_event(ModelCreated) +RSpec::Matchers.define :not_publish_event do |expected_event_class| + include RSpec::Matchers::Composable + + supports_block_expectations + + match do |proc| + raise ArgumentError, 'not_publish_event matcher only supports block expectation' unless proc.respond_to?(:call) + + @events ||= [] + + allow(Gitlab::EventStore).to receive(:publish) do |published_event| + @events << published_event + end + + proc.call + + @events.none? do |event| + event.instance_of?(expected_event_class) + end + end + + failure_message do + "expected #{expected_event_class} not to be published" + end + + chain :with do |_| # rubocop: disable Lint/UnreachableLoop + raise ArgumentError, 'not_publish_event does not permit .with to avoid ambiguity' + end + + match_when_negated do |proc| + raise ArgumentError, 'not_publish_event matcher does not support negation. Use `expect {}.to publish_event` instead' + end +end |