debian-mirror-gitlab/spec/support/matchers/event_store.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
2.7 KiB
Ruby
Raw Normal View History

2022-04-04 11:22:00 +05:30
# frozen_string_literal: true
2022-05-07 20:08:51 +05:30
RSpec::Matchers.define :publish_event do |expected_event_class|
2022-08-13 15:12:31 +05:30
include RSpec::Matchers::Composable
2022-05-07 20:08:51 +05:30
supports_block_expectations
match do |proc|
2022-11-25 23:54:43 +05:30
raise ArgumentError, 'publish_event matcher only supports block expectation' unless proc.respond_to?(:call)
2022-05-07 20:08:51 +05:30
@events ||= []
allow(Gitlab::EventStore).to receive(:publish) do |published_event|
@events << published_event
end
proc.call
@events.any? do |event|
2022-08-13 15:12:31 +05:30
event.instance_of?(expected_event_class) && match_data?(event.data, @expected_data)
2022-05-07 20:08:51 +05:30
end
2022-04-04 11:22:00 +05:30
end
2022-08-13 15:12:31 +05:30
def match_data?(actual, expected)
2022-11-25 23:54:43 +05:30
return if actual.blank? || expected.blank?
2022-08-13 15:12:31 +05:30
values_match?(actual.keys, expected.keys) &&
2022-08-27 11:52:29 +05:30
actual.keys.all? do |key|
values_match?(expected[key], actual[key])
2022-08-13 15:12:31 +05:30
end
end
2022-05-07 20:08:51 +05:30
chain :with do |expected_data|
2022-04-04 11:22:00 +05:30
@expected_data = expected_data
end
2022-05-07 20:08:51 +05:30
failure_message do
2022-11-25 23:54:43 +05:30
message = "expected #{expected_event_class} with #{@expected_data || 'no data'} to be published"
if @events.present?
<<~MESSAGE
#{message}, but only the following events were published:
#{events_list}
MESSAGE
else
"#{message}, but no events were published."
end
2022-05-07 20:08:51 +05:30
end
match_when_negated do |proc|
2022-11-25 23:54:43 +05:30
raise ArgumentError, 'publish_event matcher only supports block expectation' unless proc.respond_to?(:call)
2022-05-07 20:08:51 +05:30
allow(Gitlab::EventStore).to receive(:publish)
proc.call
expect(Gitlab::EventStore).not_to have_received(:publish).with(instance_of(expected_event_class))
end
2022-11-25 23:54:43 +05:30
def events_list
@events.map do |event|
" - #{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
2022-04-04 11:22:00 +05:30
end