Created
January 27, 2021 21:17
-
-
Save ytnk531/db11014526c5dbbad4ad37ed51aefa89 to your computer and use it in GitHub Desktop.
Rollback will not occur situation in RubyOnRails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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