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|
|
|
|
|
raise ArgumentError, 'This 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.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)
|
|
|
|
values_match?(actual.keys, expected.keys) &&
|
|
|
|
actual.keys.each do |key|
|
|
|
|
values_match?(actual[key], expected[key])
|
|
|
|
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
|
|
|
|
"expected #{expected_event_class} with #{@expected_data} to be published, but got #{@events}"
|
|
|
|
end
|
|
|
|
|
|
|
|
match_when_negated do |proc|
|
|
|
|
raise ArgumentError, 'This matcher only supports block expectation' unless proc.respond_to?(:call)
|
|
|
|
|
|
|
|
allow(Gitlab::EventStore).to receive(:publish)
|
|
|
|
|
|
|
|
proc.call
|
|
|
|
|
|
|
|
expect(Gitlab::EventStore).not_to have_received(:publish).with(instance_of(expected_event_class))
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|