Fix `RSpec/MessageChain` cop (#27776)

pull/27797/head
Matt Jankowski 2023-11-09 07:57:23 -05:00 committed by GitHub
parent dec2796a4a
commit 63c9102f8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 23 deletions

View File

@ -104,12 +104,6 @@ RSpec/LetSetup:
- 'spec/services/unsuspend_account_service_spec.rb' - 'spec/services/unsuspend_account_service_spec.rb'
- 'spec/workers/scheduler/user_cleanup_scheduler_spec.rb' - 'spec/workers/scheduler/user_cleanup_scheduler_spec.rb'
RSpec/MessageChain:
Exclude:
- 'spec/models/concerns/remotable_spec.rb'
- 'spec/models/session_activation_spec.rb'
- 'spec/models/setting_spec.rb'
RSpec/MultipleExpectations: RSpec/MultipleExpectations:
Max: 8 Max: 8

View File

@ -2,5 +2,5 @@
Fabricator(:session_activation) do Fabricator(:session_activation) do
user { Fabricate.build(:user) } user { Fabricate.build(:user) }
session_id 'MyString' session_id { sequence(:session_id) { |i| "session_id_#{i}" } }
end end

View File

@ -69,7 +69,9 @@ RSpec.describe Remotable do
context 'with an invalid URL' do context 'with an invalid URL' do
before do before do
allow(Addressable::URI).to receive_message_chain(:parse, :normalize).with(url).with(no_args).and_raise(Addressable::URI::InvalidURIError) parsed = instance_double(Addressable::URI)
allow(parsed).to receive(:normalize).with(no_args).and_raise(Addressable::URI::InvalidURIError)
allow(Addressable::URI).to receive(:parse).with(url).and_return(parsed)
end end
it 'makes no request' do it 'makes no request' do

View File

@ -98,34 +98,44 @@ RSpec.describe SessionActivation do
end end
context 'when id exists' do context 'when id exists' do
let(:id) { '1' } let!(:session_activation) { Fabricate(:session_activation) }
it 'calls where.destroy_all' do it 'destroys the record' do
expect(described_class).to receive_message_chain(:where, :destroy_all) described_class.deactivate(session_activation.session_id)
.with(session_id: id).with(no_args)
described_class.deactivate(id) expect { session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
end end
end end
end end
describe '.purge_old' do describe '.purge_old' do
it 'calls order.offset.destroy_all' do around do |example|
expect(described_class).to receive_message_chain(:order, :offset, :destroy_all) before = Rails.configuration.x.max_session_activations
.with('created_at desc').with(Rails.configuration.x.max_session_activations).with(no_args) Rails.configuration.x.max_session_activations = 1
example.run
Rails.configuration.x.max_session_activations = before
end
let!(:oldest_session_activation) { Fabricate(:session_activation, created_at: 10.days.ago) }
let!(:newest_session_activation) { Fabricate(:session_activation, created_at: 5.days.ago) }
it 'preserves the newest X records based on config' do
described_class.purge_old described_class.purge_old
expect { oldest_session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { newest_session_activation.reload }.to_not raise_error
end end
end end
describe '.exclusive' do describe '.exclusive' do
let(:id) { '1' } let!(:unwanted_session_activation) { Fabricate(:session_activation) }
let!(:wanted_session_activation) { Fabricate(:session_activation) }
it 'calls where.destroy_all' do it 'preserves supplied record and destroys all others' do
expect(described_class).to receive_message_chain(:where, :not, :destroy_all) described_class.exclusive(wanted_session_activation.session_id)
.with(session_id: id).with(no_args)
described_class.exclusive(id) expect { unwanted_session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { wanted_session_activation.reload }.to_not raise_error
end end
end end
end end

View File

@ -77,10 +77,13 @@ RSpec.describe Setting do
let(:default_value) { { default_value: 'default_value' } } let(:default_value) { { default_value: 'default_value' } }
it 'calls default_value.with_indifferent_access.merge!' do it 'calls default_value.with_indifferent_access.merge!' do
expect(default_value).to receive_message_chain(:with_indifferent_access, :merge!) indifferent_hash = instance_double(Hash, merge!: nil)
.with(db_val.value) allow(default_value).to receive(:with_indifferent_access).and_return(indifferent_hash)
described_class[key] described_class[key]
expect(default_value).to have_received(:with_indifferent_access)
expect(indifferent_hash).to have_received(:merge!).with(db_val.value)
end end
end end