Skip to content

Instantly share code, notes, and snippets.

@ytnk531
Created January 27, 2021 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytnk531/db11014526c5dbbad4ad37ed51aefa89 to your computer and use it in GitHub Desktop.
Save ytnk531/db11014526c5dbbad4ad37ed51aefa89 to your computer and use it in GitHub Desktop.
Rollback will not occur situation in RubyOnRails
require 'rails_helper'
def exec_transaction
ApplicationRecord.transaction do
User.create!(name: 'Duplicate')
User.create!(name: 'Duplicate')
end
end
def exec_transaction_rescue
ApplicationRecord.transaction do
User.create!(name: 'Duplicate')
User.create!(name: 'Duplicate')
rescue ActiveRecord::RecordInvalid
false
end
end
describe 'exec_transaction' do
context 'When first record is valid and second record is invalid' do
it 'rollbacks first record insertion' do
expect { exec_transaction }.to raise_error(
ActiveRecord::RecordInvalid,
/Name has already been taken/
)
expect(User.count).to eq 0
end
end
end
describe 'exec_transaction_rescue' do
context 'When first record is valid and second record is invalid' do
it 'does not rollbacks first record insertion' do
exec_transaction
expect(User.count).to eq 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment