Commit 93e8701d authored by Arturo Herrero's avatar Arturo Herrero

Update RSpec helper methods to *_next_instance_of

Auto-correct some references in RSpec as:
- expect_any_instance_of -> expect_next_instance_of
- allow_any_instance_of -> allow_next_instance_of

Related to https://gitlab.com/gitlab-org/gitlab/issues/34997
parent b4e3a822
......@@ -61,7 +61,9 @@ describe Analytics::CycleAnalytics::Stages::UpdateService do
context 'when the update fails' do
before do
invalid_stage = Analytics::CycleAnalytics::GroupStage.new(name: '')
expect_any_instance_of(described_class).to receive(:find_stage).and_return(invalid_stage)
expect_next_instance_of(described_class) do |instance|
expect(instance).to receive(:find_stage).and_return(invalid_stage)
end
end
it 'returns unsuccessful service response' do
......
......@@ -97,7 +97,9 @@ describe DesignManagement::DeleteDesignsService do
it 'calls repository#log_geo_updated_event' do
design_repository = EE::Gitlab::GlRepository::DESIGN.repository_resolver.call(project)
allow_any_instance_of(described_class).to receive(:repository).and_return(design_repository)
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:repository).and_return(design_repository)
end
expect(design_repository).to receive(:log_geo_updated_event)
......
......@@ -193,7 +193,9 @@ describe DesignManagement::SaveDesignsService do
it 'calls repository#log_geo_updated_event' do
expect(design_repository).to receive(:log_geo_updated_event)
allow_any_instance_of(described_class).to receive(:repository).and_return(design_repository)
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:repository).and_return(design_repository)
end
run_service
end
......
......@@ -241,7 +241,9 @@ describe DraftNotes::PublishService do
end
it 'sends notifications if all threads are resolved' do
expect_any_instance_of(MergeRequests::ResolvedDiscussionNotificationService).to receive(:execute).with(merge_request)
expect_next_instance_of(MergeRequests::ResolvedDiscussionNotificationService) do |instance|
expect(instance).to receive(:execute).with(merge_request)
end
publish
end
......
......@@ -30,7 +30,9 @@ describe Issues::CreateFromVulnerabilityDataService, '#execute' do
let(:result) { described_class.new(project, user, {}).execute }
before do
allow_any_instance_of(described_class).to receive(:can?).with(user, :create_issue, project).and_return(false)
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:can?).with(user, :create_issue, project).and_return(false)
end
end
it 'returns expected error' do
......
......@@ -19,7 +19,9 @@ describe Issues::MoveService do
let!(:hook) { create(:group_hook, group: new_project.group, issues_events: true) }
it 'executes group issue hooks' do
allow_any_instance_of(WebHookService).to receive(:execute)
allow_next_instance_of(WebHookService) do |instance|
allow(instance).to receive(:execute)
end
# Ideally, we'd test that `WebHookWorker.jobs.size` increased by 1,
# but since the entire spec run takes place in a transaction, we never
......
......@@ -398,7 +398,9 @@ describe EE::NotificationService, :mailer do
it_behaves_like 'project emails are disabled' do
before do
allow_any_instance_of(::Gitlab::Alerting::Alert).to receive(:valid?).and_return(true)
allow_next_instance_of(::Gitlab::Alerting::Alert) do |instance|
allow(instance).to receive(:valid?).and_return(true)
end
end
let(:alert_params) { { 'labels' => { 'gitlab_alert_id' => 'unknown' } } }
......
......@@ -80,7 +80,9 @@ describe Geo::ContainerRepositorySync, :geo do
describe 'execute' do
it 'determines list of tags to sync and to remove correctly' do
expect(container_repository).to receive(:delete_tag_by_digest).with('sha256:aaaaa')
expect_any_instance_of(described_class).to receive(:sync_tag).with('tag-to-sync').and_call_original
expect_next_instance_of(described_class) do |instance|
expect(instance).to receive(:sync_tag).with('tag-to-sync').and_call_original
end
described_class.new(container_repository).execute
end
......
......@@ -195,7 +195,9 @@ describe Geo::MetricsUpdateService, :geo, :prometheus do
end
it 'increments a counter when metrics fail to retrieve' do
allow_any_instance_of(Geo::NodeStatusPostService).to receive(:execute).and_return(false)
allow_next_instance_of(Geo::NodeStatusPostService) do |instance|
allow(instance).to receive(:execute).and_return(false)
end
# Run once to get the gauge set
subject.execute
......
......@@ -18,13 +18,14 @@ describe JiraConnect::SyncService do
end
def expect_jira_client_call(return_value = { 'status': 'success' })
expect_any_instance_of(Atlassian::JiraConnect::Client)
.to receive(:store_dev_info).with(
expect_next_instance_of(Atlassian::JiraConnect::Client) do |instance|
expect(instance).to receive(:store_dev_info).with(
project: project,
commits: commits,
branches: [instance_of(Gitlab::Git::Branch)],
merge_requests: merge_requests
).and_return(return_value)
end
end
def expect_log(type, message)
......
......@@ -148,7 +148,9 @@ describe MergeTrains::RefreshMergeRequestService do
context 'when it failed to create a pipeline' do
before do
allow_any_instance_of(MergeTrains::CreatePipelineService).to receive(:execute) { { result: :error, message: 'failed to create pipeline' } }
allow_next_instance_of(MergeTrains::CreatePipelineService) do |instance|
allow(instance).to receive(:execute) { { result: :error, message: 'failed to create pipeline' } }
end
end
it_behaves_like 'drops the merge request from the merge train' do
......@@ -212,7 +214,9 @@ describe MergeTrains::RefreshMergeRequestService do
before do
allow(merge_request).to receive(:mergeable_state?) { true }
merge_request.update(merge_error: 'Branch has been updated since the merge was requested.')
allow_any_instance_of(MergeRequests::MergeService).to receive(:execute) { { result: :error } }
allow_next_instance_of(MergeRequests::MergeService) do |instance|
allow(instance).to receive(:execute) { { result: :error } }
end
end
it 'does not finish merge and drops the merge request from train' do
......
......@@ -8,7 +8,9 @@ describe PathLocks::LockService do
let(:path) { 'app/models' }
it 'locks path' do
allow_any_instance_of(described_class).to receive(:can?).and_return(true)
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:can?).and_return(true)
end
described_class.new(project, current_user).execute(path)
expect(project.path_locks.find_by(path: path)).to be_truthy
......
......@@ -9,7 +9,9 @@ describe PathLocks::UnlockService do
let(:path) { path_lock.path }
it 'unlocks path' do
allow_any_instance_of(described_class).to receive(:can?).and_return(true)
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:can?).and_return(true)
end
described_class.new(project, current_user).execute(path_lock)
expect(project.path_locks.find_by(path: path)).to be_falsey
......
......@@ -236,7 +236,9 @@ describe Projects::CreateService, '#execute' do
context 'when importing Project by repo URL' do
context 'and check namespace plan is enabled' do
before do
allow_any_instance_of(EE::Project).to receive(:add_import_job)
allow_next_instance_of(EE::Project) do |instance|
allow(instance).to receive(:add_import_job)
end
enable_namespace_license_check!
end
......
......@@ -36,7 +36,9 @@ describe Projects::GitlabProjectsImportService do
context 'does not create export job' do
it 'if project not saved' do
allow_any_instance_of(Project).to receive(:saved?).and_return(false)
allow_next_instance_of(Project) do |instance|
allow(instance).to receive(:saved?).and_return(false)
end
expect(custom_template).not_to receive(:add_export_job)
......
......@@ -11,8 +11,9 @@ describe Projects::SlackApplicationInstallService do
end
def stub_slack_response_with(response)
expect_any_instance_of(Projects::SlackApplicationInstallService)
.to receive(:exchange_slack_token).and_return(response.stringify_keys)
expect_next_instance_of(Projects::SlackApplicationInstallService) do |instance|
expect(instance).to receive(:exchange_slack_token).and_return(response.stringify_keys)
end
end
def expect_slack_integration_is_created(project)
......
......@@ -30,8 +30,9 @@ describe Projects::TransferService do
include_examples 'audit event logging' do
let(:operation) { subject.execute(group) }
let(:fail_condition!) do
expect_any_instance_of(Project)
.to receive(:has_container_registry_tags?).and_return(true)
expect_next_instance_of(Project) do |instance|
expect(instance).to receive(:has_container_registry_tags?).and_return(true)
end
end
let(:attributes) do
{
......
......@@ -359,7 +359,9 @@ describe Projects::UpdateMirrorService do
it 'updates LFS objects' do
expect(Projects::LfsPointers::LfsImportService).to receive(:new).and_call_original
expect_any_instance_of(Projects::LfsPointers::LfsObjectDownloadListService).to receive(:execute).and_return({})
expect_next_instance_of(Projects::LfsPointers::LfsObjectDownloadListService) do |instance|
expect(instance).to receive(:execute).and_return({})
end
service.execute
end
......@@ -368,7 +370,9 @@ describe Projects::UpdateMirrorService do
let(:error_message) { 'error_message' }
before do
expect_any_instance_of(Projects::LfsPointers::LfsImportService).to receive(:execute).and_return(status: :error, message: error_message)
expect_next_instance_of(Projects::LfsPointers::LfsImportService) do |instance|
expect(instance).to receive(:execute).and_return(status: :error, message: error_message)
end
end
# Uncomment once https://gitlab.com/gitlab-org/gitlab-foss/issues/61834 is closed
......@@ -389,7 +393,9 @@ describe Projects::UpdateMirrorService do
end
it 'logs the error' do
expect_any_instance_of(Gitlab::UpdateMirrorServiceJsonLogger).to receive(:error).with(hash_including(error_message: error_message))
expect_next_instance_of(Gitlab::UpdateMirrorServiceJsonLogger) do |instance|
expect(instance).to receive(:error).with(hash_including(error_message: error_message))
end
subject.execute
end
......
......@@ -68,7 +68,9 @@ describe Security::StoreReportsService do
end
it 'caches vulnerability history' do
expect_any_instance_of(Gitlab::Vulnerabilities::HistoryCache).to receive(:fetch)
expect_next_instance_of(Gitlab::Vulnerabilities::HistoryCache) do |instance|
expect(instance).to receive(:fetch)
end
subject
end
......
......@@ -84,7 +84,9 @@ describe Security::SyncReportsToApprovalRulesService, '#execute' do
context "when an unexpected error occurs" do
before do
allow_any_instance_of(Gitlab::Ci::Reports::LicenseScanning::Report).to receive(:violates?).and_raise('heck')
allow_next_instance_of(Gitlab::Ci::Reports::LicenseScanning::Report) do |instance|
allow(instance).to receive(:violates?).and_raise('heck')
end
end
specify { expect(subject[:status]).to be(:error) }
......
......@@ -63,7 +63,9 @@ describe SoftwareLicensePolicies::CreateService do
context "when an argument error is raised" do
before do
allow_any_instance_of(Project).to receive(:software_license_policies).and_raise(ArgumentError)
allow_next_instance_of(Project) do |instance|
allow(instance).to receive(:software_license_policies).and_raise(ArgumentError)
end
end
specify { expect(subject.execute[:status]).to be(:error) }
......
......@@ -124,8 +124,9 @@ describe VulnerabilityFeedback::CreateService, '#execute' do
end
it 'delegates the Issue creation to CreateFromVulnerabilityDataService' do
expect_any_instance_of(Issues::CreateFromVulnerabilityDataService)
.to receive(:execute).once.and_call_original
expect_next_instance_of(Issues::CreateFromVulnerabilityDataService) do |instance|
expect(instance).to receive(:execute).once.and_call_original
end
expect(result[:status]).to eq(:success)
end
......
......@@ -20,7 +20,9 @@ module EE
def mock_group_saml(uid:)
allow(Devise).to receive(:omniauth_providers).and_return(%i(group_saml))
allow_any_instance_of(::Gitlab::Auth::Saml::OriginValidator).to receive(:gitlab_initiated?).and_return(true)
allow_next_instance_of(::Gitlab::Auth::Saml::OriginValidator) do |instance|
allow(instance).to receive(:gitlab_initiated?).and_return(true)
end
configure_group_saml_mock_auth(uid: uid)
stub_omniauth_provider(:group_saml)
end
......
......@@ -9,8 +9,9 @@ describe ClearSharedRunnersMinutesWorker do
let(:namespace) { create(:namespace) }
before do
expect_any_instance_of(described_class)
.to receive(:try_obtain_lease).and_return(true)
expect_next_instance_of(described_class) do |instance|
expect(instance).to receive(:try_obtain_lease).and_return(true)
end
end
subject { worker.perform }
......
......@@ -23,21 +23,23 @@ describe CreateGithubWebhookWorker do
end
it 'creates the webhook' do
expect_any_instance_of(Gitlab::LegacyGithubImport::Client).to receive(:create_hook)
.with(
'foo/bar',
'web',
{
url: "http://localhost#{api_v4_projects_mirror_pull_path(id: project.id)}",
content_type: 'json',
secret: project.external_webhook_token,
insecure_ssl: 1
},
{
events: %w[push pull_request],
active: true
}
)
expect_next_instance_of(Gitlab::LegacyGithubImport::Client) do |instance|
expect(instance).to receive(:create_hook)
.with(
'foo/bar',
'web',
{
url: "http://localhost#{api_v4_projects_mirror_pull_path(id: project.id)}",
content_type: 'json',
secret: project.external_webhook_token,
insecure_ssl: 1
},
{
events: %w[push pull_request],
active: true
}
)
end
subject.perform(project.id)
end
......
......@@ -12,7 +12,9 @@ describe Geo::ContainerRepositorySyncDispatchWorker, :geo, :geo_fdw, :use_sql_qu
before do
stub_current_geo_node(secondary)
stub_exclusive_lease(renew: true)
allow_any_instance_of(described_class).to receive(:over_time?).and_return(false)
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:over_time?).and_return(false)
end
stub_registry_replication_config(enabled: true)
end
......
......@@ -12,7 +12,9 @@ describe Geo::FileDownloadDispatchWorker, :geo, :geo_fdw, :use_sql_query_cache_f
before do
stub_current_geo_node(secondary)
stub_exclusive_lease(renew: true)
allow_any_instance_of(described_class).to receive(:over_time?).and_return(false)
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:over_time?).and_return(false)
end
WebMock.stub_request(:get, /primary-geo-node/).to_return(status: 200, body: "", headers: {})
end
......
......@@ -155,7 +155,9 @@ describe Geo::RepositoryShardSyncWorker, :geo, :geo_fdw, :clean_gitlab_redis_cac
unsynced_project.destroy
unsynced_project_in_restricted_group.destroy
allow_any_instance_of(described_class).to receive(:db_retrieve_batch_size).and_return(2) # Must be >1 because of the Geo::BaseSchedulerWorker#interleave
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:db_retrieve_batch_size).and_return(2) # Must be >1 because of the Geo::BaseSchedulerWorker#interleave
end
secondary.update!(repos_max_capacity: 3) # Must be more than db_retrieve_batch_size
project_list.each do |project|
......@@ -165,8 +167,12 @@ describe Geo::RepositoryShardSyncWorker, :geo, :geo_fdw, :clean_gitlab_redis_cac
.and_call_original
end
allow_any_instance_of(Geo::ProjectRegistry).to receive(:wiki_sync_due?).and_return(false)
allow_any_instance_of(Geo::RepositorySyncService).to receive(:expire_repository_caches)
allow_next_instance_of(Geo::ProjectRegistry) do |instance|
allow(instance).to receive(:wiki_sync_due?).and_return(false)
end
allow_next_instance_of(Geo::RepositorySyncService) do |instance|
allow(instance).to receive(:expire_repository_caches)
end
end
it 'tries to sync project where last attempt to sync failed' do
......@@ -216,12 +222,22 @@ describe Geo::RepositoryShardSyncWorker, :geo, :geo_fdw, :clean_gitlab_redis_cac
unsynced_project.destroy
unsynced_project_in_restricted_group.destroy
allow_any_instance_of(described_class).to receive(:db_retrieve_batch_size).and_return(2) # Must be >1 because of the Geo::BaseSchedulerWorker#interleave
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:db_retrieve_batch_size).and_return(2) # Must be >1 because of the Geo::BaseSchedulerWorker#interleave
end
secondary.update!(repos_max_capacity: 3) # Must be more than db_retrieve_batch_size
allow_any_instance_of(Project).to receive(:ensure_repository).and_raise(Gitlab::Shell::Error.new('foo'))
allow_any_instance_of(Geo::ProjectRegistry).to receive(:wiki_sync_due?).and_return(false)
allow_any_instance_of(Geo::RepositorySyncService).to receive(:expire_repository_caches)
allow_any_instance_of(Geo::ProjectHousekeepingService).to receive(:do_housekeeping)
allow_next_instance_of(Project) do |instance|
allow(instance).to receive(:ensure_repository).and_raise(Gitlab::Shell::Error.new('foo'))
end
allow_next_instance_of(Geo::ProjectRegistry) do |instance|
allow(instance).to receive(:wiki_sync_due?).and_return(false)
end
allow_next_instance_of(Geo::RepositorySyncService) do |instance|
allow(instance).to receive(:expire_repository_caches)
end
allow_next_instance_of(Geo::ProjectHousekeepingService) do |instance|
allow(instance).to receive(:do_housekeeping)
end
end
it 'tries to sync every project' do
......
......@@ -7,7 +7,9 @@ describe GeoRepositoryDestroyWorker do
it 'delegates project removal to Geo::RepositoryDestroyService' do
project = create(:project)
expect_any_instance_of(Geo::RepositoryDestroyService).to receive(:execute)
expect_next_instance_of(Geo::RepositoryDestroyService) do |instance|
expect(instance).to receive(:execute)
end
described_class.new.perform(project.id, project.name, project.path, 'default')
end
......
......@@ -91,9 +91,9 @@ describe IncidentManagement::ProcessPrometheusAlertWorker do
context 'when issue could not be created' do
before do
allow_any_instance_of(IncidentManagement::CreateIssueService)
.to receive(:execute)
.and_return( { error: true } )
allow_next_instance_of(IncidentManagement::CreateIssueService) do |instance|
allow(instance).to receive(:execute).and_return( { error: true } )
end
end
it 'does not relate issue to an event' do
......
......@@ -12,8 +12,9 @@ describe JiraConnect::SyncBranchWorker do
subject { described_class.new.perform(project_id, branch_name, commit_shas) }
def expect_jira_sync_service_execute(args)
expect_any_instance_of(JiraConnect::SyncService)
.to receive(:execute).with(args)
expect_next_instance_of(JiraConnect::SyncService) do |instance|
expect(instance).to receive(:execute).with(args)
end
end
it 'calls JiraConnect::SyncService#execute' do
......
......@@ -16,7 +16,9 @@ describe ProjectImportScheduleWorker do
it 'schedules an import for a project' do
import_state = create(:import_state)
allow_any_instance_of(EE::Project).to receive(:add_import_job).and_return(nil)
allow_next_instance_of(EE::Project) do |instance|
allow(instance).to receive(:add_import_job).and_return(nil)
end
expect do
subject.perform(import_state.project_id)
......
......@@ -13,13 +13,17 @@ describe RepositoryUpdateMirrorWorker do
end
it 'sets status as finished when update mirror service executes successfully' do
expect_any_instance_of(Projects::UpdateMirrorService).to receive(:execute).and_return(status: :success)
expect_next_instance_of(Projects::UpdateMirrorService) do |instance|
expect(instance).to receive(:execute).and_return(status: :success)
end
expect { subject.perform(project.id) }.to change { import_state.reload.status }.to('finished')
end
it 'sets status as failed when update mirror service executes with errors' do
allow_any_instance_of(Projects::UpdateMirrorService).to receive(:execute).and_return(status: :error, message: 'error')
allow_next_instance_of(Projects::UpdateMirrorService) do |instance|
allow(instance).to receive(:execute).and_return(status: :error, message: 'error')
end
expect { subject.perform(project.id) }.to raise_error(RepositoryUpdateMirrorWorker::UpdateError, 'error')
expect(project.reload.import_status).to eq('failed')
......@@ -34,7 +38,9 @@ describe RepositoryUpdateMirrorWorker do
end
it 'marks mirror as failed when an error occurs' do
allow_any_instance_of(Projects::UpdateMirrorService).to receive(:execute).and_raise(RuntimeError)
allow_next_instance_of(Projects::UpdateMirrorService) do |instance|
allow(instance).to receive(:execute).and_raise(RuntimeError)
end
expect { subject.perform(project.id) }.to raise_error(RepositoryUpdateMirrorWorker::UpdateError)
expect(import_state.reload.status).to eq('failed')
......@@ -45,7 +51,9 @@ describe RepositoryUpdateMirrorWorker do
let(:import_state) { create(:import_state, :mirror, :started, project: started_project, jid: jid) }
it 'sets status as finished when update mirror service executes successfully' do
expect_any_instance_of(Projects::UpdateMirrorService).to receive(:execute).and_return(status: :success)
expect_next_instance_of(Projects::UpdateMirrorService) do |instance|
expect(instance).to receive(:execute).and_return(status: :success)
end
expect { subject.perform(started_project.id) }.to change { import_state.reload.status }.to('finished')
end
......
......@@ -9,8 +9,9 @@ describe ProjectUpdateRepositoryStorageWorker do
describe "#perform" do
it "calls the update repository storage service" do
expect_any_instance_of(Projects::UpdateRepositoryStorageService)
.to receive(:execute).with('new_storage')
expect_next_instance_of(Projects::UpdateRepositoryStorageService) do |instance|
expect(instance).to receive(:execute).with('new_storage')
end
subject.perform(project.id, 'new_storage')
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