diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2019-02-04 14:39:54 +0100 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2019-02-05 14:44:41 +0100 |
commit | ccd8a9b2821c964b85533c253430041712ef195e (patch) | |
tree | fb39012eb785dcfaa6b46a062e32b3fe4a042d53 /spec | |
parent | d6b39ea7fb31e243c59b0ca66b0fd4de3296f004 (diff) | |
download | gitlab-ce-ccd8a9b2821c964b85533c253430041712ef195e.tar.gz |
Adds helper for `find_or_create_by` in transaction
This allows us to call `find_or_create_by` on all models and scopes.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/models/application_record_spec.rb | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/spec/models/application_record_spec.rb b/spec/models/application_record_spec.rb index 68aed387bfc..ca23f581fdc 100644 --- a/spec/models/application_record_spec.rb +++ b/spec/models/application_record_spec.rb @@ -10,4 +10,14 @@ describe ApplicationRecord do expect(User.id_in(records.last(2).map(&:id))).to eq(records.last(2)) end end + + describe '#safe_find_or_create_by' do + it 'creates the user avoiding race conditions' do + expect(Suggestion).to receive(:find_or_create_by).and_raise(ActiveRecord::RecordNotUnique) + allow(Suggestion).to receive(:find_or_create_by).and_call_original + + expect { Suggestion.safe_find_or_create_by(build(:suggestion).attributes) } + .to change { Suggestion.count }.by(1) + end + end end |