diff options
Diffstat (limited to 'spec/models/application_record_spec.rb')
-rw-r--r-- | spec/models/application_record_spec.rb | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/spec/models/application_record_spec.rb b/spec/models/application_record_spec.rb index cc314d9077d..d9ab326505b 100644 --- a/spec/models/application_record_spec.rb +++ b/spec/models/application_record_spec.rb @@ -38,6 +38,21 @@ RSpec.describe ApplicationRecord do expect { Suggestion.safe_find_or_create_by(build(:suggestion).attributes) } .to change { Suggestion.count }.by(1) end + + it 'passes a block to find_or_create_by' do + attributes = build(:suggestion).attributes + + expect do |block| + Suggestion.safe_find_or_create_by(attributes, &block) + end.to yield_with_args(an_object_having_attributes(attributes)) + end + + it 'does not create a record when is not valid' do + raw_usage_data = RawUsageData.safe_find_or_create_by({ recorded_at: nil }) + + expect(raw_usage_data.id).to be_nil + expect(raw_usage_data).not_to be_valid + end end describe '.safe_find_or_create_by!' do @@ -51,6 +66,14 @@ RSpec.describe ApplicationRecord do it 'raises a validation error if the record was not persisted' do expect { Suggestion.find_or_create_by!(note: nil) }.to raise_error(ActiveRecord::RecordInvalid) end + + it 'passes a block to find_or_create_by' do + attributes = build(:suggestion).attributes + + expect do |block| + Suggestion.safe_find_or_create_by!(attributes, &block) + end.to yield_with_args(an_object_having_attributes(attributes)) + end end describe '.underscore' do |