Commit a7d17cad authored by Albert Salim's avatar Albert Salim Committed by Fabio Pitino

Improve matcher for publishing event

parent f83b68da
...@@ -28,16 +28,7 @@ RSpec.describe Ci::JobArtifacts::DestroyAllExpiredService, :clean_gitlab_redis_s ...@@ -28,16 +28,7 @@ RSpec.describe Ci::JobArtifacts::DestroyAllExpiredService, :clean_gitlab_redis_s
end end
it 'publishes Ci::JobArtifactsDeletedEvent' do it 'publishes Ci::JobArtifactsDeletedEvent' do
event = double(:event) expect { subject }.to publish_event(Ci::JobArtifactsDeletedEvent).with(event_data)
expect(Ci::JobArtifactsDeletedEvent)
.to receive(:new)
.with(data: event_data)
.and_return(event)
expect(Gitlab::EventStore).to receive(:publish).with(event)
subject
end end
end end
......
...@@ -21,16 +21,7 @@ RSpec.describe Ci::JobArtifacts::DestroyBatchService do ...@@ -21,16 +21,7 @@ RSpec.describe Ci::JobArtifacts::DestroyBatchService do
end end
it 'publishes Ci::JobArtifactsDeletedEvent' do it 'publishes Ci::JobArtifactsDeletedEvent' do
event = double(:event) expect { subject }.to publish_event(Ci::JobArtifactsDeletedEvent).with(event_data)
expect(Ci::JobArtifactsDeletedEvent)
.to receive(:new)
.with(data: event_data)
.and_return(event)
expect(Gitlab::EventStore).to receive(:publish).with(event)
subject
end end
context 'with Geo replication' do context 'with Geo replication' do
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Projects::DestroyService, :aggregate_failures do RSpec.describe Projects::DestroyService, :aggregate_failures, :event_store_publisher do
include ProjectForksHelper include ProjectForksHelper
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
...@@ -15,7 +15,6 @@ RSpec.describe Projects::DestroyService, :aggregate_failures do ...@@ -15,7 +15,6 @@ RSpec.describe Projects::DestroyService, :aggregate_failures do
before do before do
stub_container_registry_config(enabled: true) stub_container_registry_config(enabled: true)
stub_container_registry_tags(repository: :any, tags: []) stub_container_registry_tags(repository: :any, tags: [])
allow(Gitlab::EventStore).to receive(:publish)
end end
shared_examples 'deleting the project' do shared_examples 'deleting the project' do
...@@ -30,11 +29,8 @@ RSpec.describe Projects::DestroyService, :aggregate_failures do ...@@ -30,11 +29,8 @@ RSpec.describe Projects::DestroyService, :aggregate_failures do
it 'publishes a ProjectDeleted event with project id and namespace id' do it 'publishes a ProjectDeleted event with project id and namespace id' do
expected_data = { project_id: project.id, namespace_id: project.namespace_id } 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
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 # frozen_string_literal: true
RSpec::Matchers.define :event_type do |event_class| RSpec::Matchers.define :publish_event do |expected_event_class|
match do |actual| supports_block_expectations
actual.instance_of?(event_class) &&
actual.data == @expected_data 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 end
chain :containing do |expected_data| chain :with do |expected_data|
@expected_data = expected_data @expected_data = expected_data
end 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 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