From 6c5a2233a8d0d3add76645313abf7a9f4f30016d Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 12 Jul 2023 04:20:10 -0400 Subject: [PATCH] Fix `RSpec/StubbedMock` cop (#25552) Co-authored-by: Claire --- .rubocop_todo.yml | 11 ----------- spec/controllers/api/base_controller_spec.rb | 3 ++- spec/controllers/api/v1/media_controller_spec.rb | 4 ++-- .../auth/registrations_controller_spec.rb | 6 ++++-- spec/helpers/application_helper_spec.rb | 13 ++++++------- spec/lib/status_filter_spec.rb | 4 ++-- spec/lib/status_finder_spec.rb | 7 +++++-- spec/lib/webfinger_resource_spec.rb | 3 ++- .../activitypub/process_collection_service_spec.rb | 6 +++--- 9 files changed, 26 insertions(+), 31 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9de96aa5bc..83c0ef0f00 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -367,17 +367,6 @@ RSpec/PendingWithoutReason: Exclude: - 'spec/models/account_spec.rb' -RSpec/StubbedMock: - Exclude: - - 'spec/controllers/api/base_controller_spec.rb' - - 'spec/controllers/api/v1/media_controller_spec.rb' - - 'spec/controllers/auth/registrations_controller_spec.rb' - - 'spec/helpers/application_helper_spec.rb' - - 'spec/lib/status_filter_spec.rb' - - 'spec/lib/status_finder_spec.rb' - - 'spec/lib/webfinger_resource_spec.rb' - - 'spec/services/activitypub/process_collection_service_spec.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). Rails/ApplicationController: Exclude: diff --git a/spec/controllers/api/base_controller_spec.rb b/spec/controllers/api/base_controller_spec.rb index 9f3ba81474..db1e8777f7 100644 --- a/spec/controllers/api/base_controller_spec.rb +++ b/spec/controllers/api/base_controller_spec.rb @@ -88,10 +88,11 @@ describe Api::BaseController do Mastodon::NotPermittedError => 403, }.each do |error, code| it "Handles error class of #{error}" do - expect(FakeService).to receive(:new).and_raise(error) + allow(FakeService).to receive(:new).and_raise(error) get 'error' expect(response).to have_http_status(code) + expect(FakeService).to have_received(:new) end end end diff --git a/spec/controllers/api/v1/media_controller_spec.rb b/spec/controllers/api/v1/media_controller_spec.rb index 79a9d1474a..94b2a0a98f 100644 --- a/spec/controllers/api/v1/media_controller_spec.rb +++ b/spec/controllers/api/v1/media_controller_spec.rb @@ -16,7 +16,7 @@ RSpec.describe Api::V1::MediaController do describe 'with paperclip errors' do context 'when imagemagick cant identify the file type' do it 'returns http 422' do - expect_any_instance_of(Account).to receive_message_chain(:media_attachments, :create!).and_raise(Paperclip::Errors::NotIdentifiedByImageMagickError) + allow_any_instance_of(Account).to receive_message_chain(:media_attachments, :create!).and_raise(Paperclip::Errors::NotIdentifiedByImageMagickError) post :create, params: { file: fixture_file_upload('attachment.jpg', 'image/jpeg') } expect(response).to have_http_status(422) @@ -25,7 +25,7 @@ RSpec.describe Api::V1::MediaController do context 'when there is a generic error' do it 'returns http 422' do - expect_any_instance_of(Account).to receive_message_chain(:media_attachments, :create!).and_raise(Paperclip::Error) + allow_any_instance_of(Account).to receive_message_chain(:media_attachments, :create!).and_raise(Paperclip::Error) post :create, params: { file: fixture_file_upload('attachment.jpg', 'image/jpeg') } expect(response).to have_http_status(500) diff --git a/spec/controllers/auth/registrations_controller_spec.rb b/spec/controllers/auth/registrations_controller_spec.rb index ec82cd07b6..a9b24a1004 100644 --- a/spec/controllers/auth/registrations_controller_spec.rb +++ b/spec/controllers/auth/registrations_controller_spec.rb @@ -15,20 +15,22 @@ RSpec.describe Auth::RegistrationsController do it 'redirects if it is in single user mode while it is open for registration' do Fabricate(:account) Setting.registrations_mode = 'open' - expect(Rails.configuration.x).to receive(:single_user_mode).and_return(true) + allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true) get path expect(response).to redirect_to '/' + expect(Rails.configuration.x).to have_received(:single_user_mode) end it 'redirects if it is not open for registration while it is not in single user mode' do Setting.registrations_mode = 'none' - expect(Rails.configuration.x).to receive(:single_user_mode).and_return(false) + allow(Rails.configuration.x).to receive(:single_user_mode).and_return(false) get path expect(response).to redirect_to '/' + expect(Rails.configuration.x).to have_received(:single_user_mode) end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index d191265cad..d0b2900d62 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -77,19 +77,17 @@ describe ApplicationHelper do describe 'open_registrations?' do it 'returns true when open for registrations' do - without_partial_double_verification do - expect(Setting).to receive(:registrations_mode).and_return('open') - end + allow(Setting).to receive(:[]).with('registrations_mode').and_return('open') expect(helper.open_registrations?).to be true + expect(Setting).to have_received(:[]).with('registrations_mode') end it 'returns false when closed for registrations' do - without_partial_double_verification do - expect(Setting).to receive(:registrations_mode).and_return('none') - end + allow(Setting).to receive(:[]).with('registrations_mode').and_return('none') expect(helper.open_registrations?).to be false + expect(Setting).to have_received(:[]).with('registrations_mode') end end @@ -296,8 +294,9 @@ describe ApplicationHelper do it 'returns site title on production environment' do Setting.site_title = 'site title' - expect(Rails.env).to receive(:production?).and_return(true) + allow(Rails.env).to receive(:production?).and_return(true) expect(helper.title).to eq 'site title' + expect(Rails.env).to have_received(:production?) end end end diff --git a/spec/lib/status_filter_spec.rb b/spec/lib/status_filter_spec.rb index 98e2ef913a..c994ad419f 100644 --- a/spec/lib/status_filter_spec.rb +++ b/spec/lib/status_filter_spec.rb @@ -23,7 +23,7 @@ describe StatusFilter do context 'when status policy does not allow show' do it 'filters the status' do - expect_any_instance_of(StatusPolicy).to receive(:show?).and_return(false) + allow_any_instance_of(StatusPolicy).to receive(:show?).and_return(false) expect(filter).to be_filtered end @@ -74,7 +74,7 @@ describe StatusFilter do context 'when status policy does not allow show' do it 'filters the status' do - expect_any_instance_of(StatusPolicy).to receive(:show?).and_return(false) + allow_any_instance_of(StatusPolicy).to receive(:show?).and_return(false) expect(filter).to be_filtered end diff --git a/spec/lib/status_finder_spec.rb b/spec/lib/status_finder_spec.rb index 61483f4bfe..53f5039af9 100644 --- a/spec/lib/status_finder_spec.rb +++ b/spec/lib/status_finder_spec.rb @@ -18,10 +18,13 @@ describe StatusFinder do it 'raises an error if action is not :show' do recognized = Rails.application.routes.recognize_path(url) - expect(recognized).to receive(:[]).with(:action).and_return(:create) - expect(Rails.application.routes).to receive(:recognize_path).with(url).and_return(recognized) + allow(recognized).to receive(:[]).with(:action).and_return(:create) + allow(Rails.application.routes).to receive(:recognize_path).with(url).and_return(recognized) expect { subject.status }.to raise_error(ActiveRecord::RecordNotFound) + + expect(Rails.application.routes).to have_received(:recognize_path) + expect(recognized).to have_received(:[]) end end diff --git a/spec/lib/webfinger_resource_spec.rb b/spec/lib/webfinger_resource_spec.rb index 2cad04fccd..558a318927 100644 --- a/spec/lib/webfinger_resource_spec.rb +++ b/spec/lib/webfinger_resource_spec.rb @@ -27,13 +27,14 @@ describe WebfingerResource do recognized = Rails.application.routes.recognize_path(resource) allow(recognized).to receive(:[]).with(:controller).and_return('accounts') allow(recognized).to receive(:[]).with(:username).and_return('alice') - expect(recognized).to receive(:[]).with(:action).and_return('create') + allow(recognized).to receive(:[]).with(:action).and_return('create') expect(Rails.application.routes).to receive(:recognize_path).with(resource).and_return(recognized).at_least(:once) expect do described_class.new(resource).username end.to raise_error(ActiveRecord::RecordNotFound) + expect(recognized).to have_received(:[]).exactly(3).times end it 'raises with a string that doesnt start with URL' do diff --git a/spec/services/activitypub/process_collection_service_spec.rb b/spec/services/activitypub/process_collection_service_spec.rb index 1433d0c505..3cd60a6195 100644 --- a/spec/services/activitypub/process_collection_service_spec.rb +++ b/spec/services/activitypub/process_collection_service_spec.rb @@ -70,7 +70,7 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do let(:forwarder) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/other_account') } it 'does not process payload if no signature exists' do - expect_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(nil) + allow_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(nil) expect(ActivityPub::Activity).to_not receive(:factory) subject.call(json, forwarder) @@ -79,7 +79,7 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do it 'processes payload with actor if valid signature exists' do payload['signature'] = { 'type' => 'RsaSignature2017' } - expect_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(actor) + allow_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(actor) expect(ActivityPub::Activity).to receive(:factory).with(instance_of(Hash), actor, instance_of(Hash)) subject.call(json, forwarder) @@ -88,7 +88,7 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do it 'does not process payload if invalid signature exists' do payload['signature'] = { 'type' => 'RsaSignature2017' } - expect_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(nil) + allow_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(nil) expect(ActivityPub::Activity).to_not receive(:factory) subject.call(json, forwarder)