Skip to content
代码片段 群组 项目
未验证 提交 39326557 编辑于 作者: Dzmitry Meshcharakou's avatar Dzmitry Meshcharakou 提交者: GitLab
浏览文件

Merge branch 'kassio/improve-rspec-matches-for-gitlab-event-store' into 'master'

No related branches found
No related tags found
无相关合并请求
# frozen_string_literal: true # frozen_string_literal: true
RSpec::Matchers.define :publish_event do |expected_event_class| module RSpec
include RSpec::Matchers::Composable module PublishedGitlabEventStoreEvents
class FakeGitlabEventStore
include ::RSpec::Mocks::ExampleMethods
attr_reader :events
def initialize
@events = []
allow(Gitlab::EventStore).to receive(:publish) do |published_event|
@events << published_event
end
end
end
supports_block_expectations # Ensure to use the same stubbed Gitlab::EventStore within the test example, this
# way the list of published events is shared within the example enabling us to
# use .(not_)to publish_event multiple times and also mix it with .[to/and] not_publish_event
def self.setup(test)
@tests ||= Hash.new { |h, k| h[k] = [] }.tap(&:compare_by_identity)
@tests[test] = FakeGitlabEventStore.new
end
match do |proc| def self.on(test)
raise ArgumentError, 'publish_event matcher only supports block expectation' unless proc.respond_to?(:call) @tests[test]&.events
end
end
@events ||= [] Matchers.define :publish_event do |expected_event_class|
include RSpec::Matchers::Composable
allow(Gitlab::EventStore).to receive(:publish) do |published_event| supports_block_expectations
@events << published_event
end
proc.call match do |proc|
raise ArgumentError, 'publish_event matcher only supports block expectation' unless proc.respond_to?(:call)
@events.any? do |event| RSpec::PublishedGitlabEventStoreEvents.setup(@matcher_execution_context)
event.instance_of?(expected_event_class) && match_data?(event.data, @expected_data)
end
end
def match_data?(actual, expected) proc.call
return if actual.blank? || expected.blank?
values_match?(actual.keys, expected.keys) && RSpec::PublishedGitlabEventStoreEvents.on(@matcher_execution_context).any? do |event|
actual.keys.all? do |key| event.instance_of?(expected_event_class) && match_data?(event.data, @expected_data)
values_match?(expected[key], actual[key])
end end
end end
chain :with do |expected_data| chain :with do |expected_data|
@expected_data = expected_data.with_indifferent_access @expected_data = expected_data.with_indifferent_access
end end
failure_message do failure_message do
message = "expected #{expected_event_class} with #{@expected_data || 'no data'} to be published" message = "expected #{expected_event_class} with #{@expected_data || 'no data'} to be published"
if @events.present? if RSpec::PublishedGitlabEventStoreEvents.on(@matcher_execution_context).present?
<<~MESSAGE <<~MESSAGE
#{message}, but only the following events were published: #{message}, but only the following events were published:
#{events_list} #{published_events_description}
MESSAGE MESSAGE
else else
"#{message}, but no events were published." "#{message}, but no events were published."
end
end end
end
match_when_negated do |proc| failure_message_when_negated do
raise ArgumentError, 'publish_event matcher only supports block expectation' unless proc.respond_to?(:call) "expected #{expected_event_class} not to be published"
end
allow(Gitlab::EventStore).to receive(:publish) private
proc.call def match_data?(actual, expected)
return if actual.blank? || expected.blank?
expect(Gitlab::EventStore).not_to have_received(:publish).with(instance_of(expected_event_class)) values_match?(actual.keys, expected.keys) &&
end actual.keys.all? { |key| values_match?(expected[key], actual[key]) }
end
def events_list def published_events_description
@events.map do |event| RSpec::PublishedGitlabEventStoreEvents.on(@matcher_execution_context).map do |event|
" - #{event.class.name} with #{event.data}" " - #{event.class.name} with #{event.data}"
end.join("\n") end.join("\n")
end
end 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 # 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)
Matchers.define :not_publish_event do |expected_event_class|
include RSpec::Matchers::Composable
match do |proc| supports_block_expectations
raise ArgumentError, 'not_publish_event matcher only supports block expectation' unless proc.respond_to?(:call)
@events ||= [] match do |proc|
raise ArgumentError, 'not_publish_event matcher only supports block expectation' unless proc.respond_to?(:call)
allow(Gitlab::EventStore).to receive(:publish) do |published_event| RSpec::PublishedGitlabEventStoreEvents.setup(@matcher_execution_context)
@events << published_event
end
proc.call proc.call
@events.none? do |event| RSpec::PublishedGitlabEventStoreEvents.on(@matcher_execution_context).none? do |event|
event.instance_of?(expected_event_class) event.instance_of?(expected_event_class)
end
end end
end
failure_message do failure_message do
"expected #{expected_event_class} not to be published" "expected #{expected_event_class} not to be published"
end end
chain :with do |_| # rubocop: disable Lint/UnreachableLoop chain :with do |_| # rubocop: disable Lint/UnreachableLoop -- false positive
raise ArgumentError, 'not_publish_event does not permit .with to avoid ambiguity' raise ArgumentError, 'not_publish_event does not permit .with to avoid ambiguity'
end end
match_when_negated do |proc| match_when_negated do |_proc|
raise ArgumentError, 'not_publish_event matcher does not support negation. Use `expect {}.to publish_event` instead' raise ArgumentError,
'not_publish_event matcher does not support negation. Use `expect {}.to publish_event` instead'
end
end end
end end
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
require 'fast_spec_helper' require 'fast_spec_helper'
require 'json_schemer' require 'json_schemer'
require 'oj'
load File.expand_path('../../../spec/support/matchers/event_store.rb', __dir__) load File.expand_path('../../../spec/support/matchers/event_store.rb', __dir__)
...@@ -123,4 +124,24 @@ def publishing_event(event_type, data = {}) ...@@ -123,4 +124,24 @@ def publishing_event(event_type, data = {})
.to raise_error('expected FakeEventType1 not to be published') .to raise_error('expected FakeEventType1 not to be published')
end end
end end
it 'validates with published_event and not_publish_event' do
matcher = -> do
expect { publishing_event(FakeEventType1, { 'id' => 1 }) }
.to publish_event(FakeEventType1).with('id' => 1)
.and not_publish_event(FakeEventType2)
end
expect(&matcher).not_to raise_error
end
it 'validates with not_publish_event and published_event' do
matcher = -> do
expect { publishing_event(FakeEventType2, { 'id' => 1 }) }
.to not_publish_event(FakeEventType1)
.and publish_event(FakeEventType2).with('id' => 1)
end
expect(&matcher).not_to raise_error
end
end end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册