Commit db3a5276 authored by Fabio Pitino's avatar Fabio Pitino

Merge branch '350129-improve-matcher-for-publishing-event' into 'master'

Improve matcher for publishing event

See merge request gitlab-org/gitlab!80358
parents 256b36a7 a7d17cad
......@@ -28,16 +28,7 @@ RSpec.describe Ci::JobArtifacts::DestroyAllExpiredService, :clean_gitlab_redis_s
end
it 'publishes Ci::JobArtifactsDeletedEvent' do
event = double(:event)
expect(Ci::JobArtifactsDeletedEvent)
.to receive(:new)
.with(data: event_data)
.and_return(event)
expect(Gitlab::EventStore).to receive(:publish).with(event)
subject
expect { subject }.to publish_event(Ci::JobArtifactsDeletedEvent).with(event_data)
end
end
......
......@@ -21,16 +21,7 @@ RSpec.describe Ci::JobArtifacts::DestroyBatchService do
end
it 'publishes Ci::JobArtifactsDeletedEvent' do
event = double(:event)
expect(Ci::JobArtifactsDeletedEvent)
.to receive(:new)
.with(data: event_data)
.and_return(event)
expect(Gitlab::EventStore).to receive(:publish).with(event)
subject
expect { subject }.to publish_event(Ci::JobArtifactsDeletedEvent).with(event_data)
end
context 'with Geo replication' do
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe Projects::DestroyService, :aggregate_failures do
RSpec.describe Projects::DestroyService, :aggregate_failures, :event_store_publisher do
include ProjectForksHelper
let_it_be(:user) { create(:user) }
......@@ -15,7 +15,6 @@ RSpec.describe Projects::DestroyService, :aggregate_failures do
before do
stub_container_registry_config(enabled: true)
stub_container_registry_tags(repository: :any, tags: [])
allow(Gitlab::EventStore).to receive(:publish)
end
shared_examples 'deleting the project' do
......@@ -30,11 +29,8 @@ RSpec.describe Projects::DestroyService, :aggregate_failures do
it 'publishes a ProjectDeleted event with project id and namespace id' do
expected_data = { project_id: project.id, namespace_id: project.namespace_id }
expect(Gitlab::EventStore)
.to receive(:publish)
.with(event_type(Projects::ProjectDeletedEvent).containing(expected_data))
destroy_project(project, user, {})
expect { destroy_project(project, user, {}) }.to publish_event(Projects::ProjectDeletedEvent).with(expected_data)
end
end
......
# frozen_string_literal: true
RSpec.configure do |config|
config.before(:each, :event_store_publisher) do
allow(Gitlab::EventStore).to receive(:publish)
end
end
# frozen_string_literal: true
RSpec::Matchers.define :event_type do |event_class|
match do |actual|
actual.instance_of?(event_class) &&
actual.data == @expected_data
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)
@events ||= []
allow(Gitlab::EventStore).to receive(:publish) do |published_event|
@events << published_event
end
proc.call
@events.any? do |event|
event.instance_of?(expected_event_class) && event.data == @expected_data
end
end
chain :containing do |expected_data|
chain :with do |expected_data|
@expected_data = expected_data
end
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
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment