Commit 60ed3271 authored by Suraj's avatar Suraj

Fixed Rails Save Bang offenses in few spec/models/* files

parent a7608d29
......@@ -165,9 +165,6 @@ Rails/SaveBang:
- 'spec/lib/gitlab/middleware/go_spec.rb'
- 'spec/lib/gitlab/shard_health_cache_spec.rb'
- 'spec/mailers/notify_spec.rb'
- 'spec/models/appearance_spec.rb'
- 'spec/models/application_record_spec.rb'
- 'spec/models/application_setting_spec.rb'
- 'spec/models/clusters/applications/helm_spec.rb'
- 'spec/models/container_repository_spec.rb'
- 'spec/models/design_management/version_spec.rb'
......
......@@ -15,7 +15,7 @@ RSpec.describe Appearance do
create(:appearance)
new_row = build(:appearance)
new_row.save
expect { new_row.save! }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Only 1 appearances row can exist')
expect(new_row.valid?).to eq(false)
end
......@@ -39,7 +39,7 @@ RSpec.describe Appearance do
end
it 'returns the path when the upload has been orphaned' do
appearance.send(logo_type).upload.destroy
appearance.send(logo_type).upload.destroy!
appearance.reload
expect(appearance.send("#{logo_type}_path")).to eq(expected_path)
......
......@@ -13,20 +13,23 @@ RSpec.describe ApplicationRecord do
describe '.safe_ensure_unique' do
let(:model) { build(:suggestion) }
let_it_be(:note) { create(:diff_note_on_merge_request) }
let(:klass) { model.class }
before do
allow(model).to receive(:save).and_raise(ActiveRecord::RecordNotUnique)
allow(model).to receive(:save!).and_raise(ActiveRecord::RecordNotUnique)
end
it 'returns false when ActiveRecord::RecordNotUnique is raised' do
expect(model).to receive(:save).once
expect(klass.safe_ensure_unique { model.save }).to be_falsey
expect(model).to receive(:save!).once
model.note_id = note.id
expect(klass.safe_ensure_unique { model.save! }).to be_falsey
end
it 'retries based on retry count specified' do
expect(model).to receive(:save).exactly(3).times
expect(klass.safe_ensure_unique(retries: 2) { model.save }).to be_falsey
expect(model).to receive(:save!).exactly(3).times
model.note_id = note.id
expect(klass.safe_ensure_unique(retries: 2) { model.save! }).to be_falsey
end
end
......
......@@ -252,7 +252,9 @@ RSpec.describe ApplicationSetting do
context "when user accepted let's encrypt terms of service" do
before do
setting.update(lets_encrypt_terms_of_service_accepted: true)
expect {
setting.update!(lets_encrypt_terms_of_service_accepted: true)
}.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Lets encrypt notification email can't be blank")
end
it { is_expected.not_to allow_value(nil).for(:lets_encrypt_notification_email) }
......@@ -302,26 +304,30 @@ RSpec.describe ApplicationSetting do
describe 'default_artifacts_expire_in' do
it 'sets an error if it cannot parse' do
setting.update(default_artifacts_expire_in: 'a')
expect {
setting.update!(default_artifacts_expire_in: 'a')
}.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Default artifacts expire in is not a correct duration")
expect_invalid
end
it 'sets an error if it is blank' do
setting.update(default_artifacts_expire_in: ' ')
expect {
setting.update!(default_artifacts_expire_in: ' ')
}.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Default artifacts expire in can't be blank")
expect_invalid
end
it 'sets the value if it is valid' do
setting.update(default_artifacts_expire_in: '30 days')
setting.update!(default_artifacts_expire_in: '30 days')
expect(setting).to be_valid
expect(setting.default_artifacts_expire_in).to eq('30 days')
end
it 'sets the value if it is 0' do
setting.update(default_artifacts_expire_in: '0')
setting.update!(default_artifacts_expire_in: '0')
expect(setting).to be_valid
expect(setting.default_artifacts_expire_in).to eq('0')
......@@ -400,18 +406,18 @@ RSpec.describe ApplicationSetting do
context 'auto_devops_domain setting' do
context 'when auto_devops_enabled? is true' do
before do
setting.update(auto_devops_enabled: true)
setting.update!(auto_devops_enabled: true)
end
it 'can be blank' do
setting.update(auto_devops_domain: '')
setting.update!(auto_devops_domain: '')
expect(setting).to be_valid
end
context 'with a valid value' do
before do
setting.update(auto_devops_domain: 'domain.com')
setting.update!(auto_devops_domain: 'domain.com')
end
it 'is valid' do
......@@ -421,7 +427,9 @@ RSpec.describe ApplicationSetting do
context 'with an invalid value' do
before do
setting.update(auto_devops_domain: 'definitelynotahostname')
expect {
setting.update!(auto_devops_domain: 'definitelynotahostname')
}.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Auto devops domain is not a fully qualified domain name")
end
it 'is invalid' do
......
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