From 04151efcac24a921424bd540ca6479b3eabd018c Mon Sep 17 00:00:00 2001 From: Stef Schenkelaars Date: Sun, 12 Jul 2020 21:14:51 +0200 Subject: [PATCH] Remove inconsistencies from active storage matcher specs --- .../have_attached_matcher_spec.rb | 70 ++++++++----------- 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/spec/unit/shoulda/matchers/active_record/have_attached_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/have_attached_matcher_spec.rb index d1fa18368..2b6597357 100644 --- a/spec/unit/shoulda/matchers/active_record/have_attached_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/have_attached_matcher_spec.rb @@ -24,7 +24,10 @@ t.index [:record_type, :record_id, :name, :blob_id], name: 'index_active_storage_attachments_uniqueness', unique: true - t.foreign_key :active_storage_blobs, column: :blob_id + + # The original rails migration has a foreign key. + # Since this messes up the clearing of the database, it's removed here. + # t.foreign_key :active_storage_blobs, column: :blob_id end end @@ -38,21 +41,18 @@ end context 'when the attached exists on the model' do - let(:record) do - record_has_one_attached(:avatar) - end it 'matches' do + record = record_having_one_attached(:avatar) expect { have_one_attached(:avatar) }. to match_against(record). or_fail_with(<<-MESSAGE) Did not expect User to have a has_one_attached called avatar, but it does. MESSAGE end + context 'and the reader attribute does not exist' do - let(:record) do - record_has_one_attached(:avatar, remove_reader: true) - end it 'matches' do + record = record_having_one_attached(:avatar, remove_reader: true) expect { have_one_attached(:avatar) }. not_to match_against(record). and_fail_with(<<-MESSAGE) @@ -61,11 +61,10 @@ MESSAGE end end + context 'and the writer attribute does not exist' do - let(:record) do - record_has_one_attached(:avatar, remove_writer: true) - end it 'matches' do + record = record_having_one_attached(:avatar, remove_writer: true) expect { have_one_attached(:avatar) }. not_to match_against(record). and_fail_with(<<-MESSAGE) @@ -74,11 +73,10 @@ MESSAGE end end + context 'and the attachments association does not exist' do - let(:record) do - record_has_one_attached(:avatar, remove_attachments: true) - end it 'matches' do + record = record_having_one_attached(:avatar, remove_attachments: true) expect { have_one_attached(:avatar) }. not_to match_against(record). and_fail_with(<<-MESSAGE) @@ -87,24 +85,22 @@ MESSAGE end end + context 'and the blobs association is invalid' do - let(:record) do - record_has_one_attached(:avatar, invalidate_blobs: true) - end it 'matches' do + record = record_having_one_attached(:avatar, invalidate_blobs: true) expect { have_one_attached(:avatar) }. not_to match_against(record). and_fail_with(<<-MESSAGE) Expected User to have a has_one_attached called avatar, but this could not be proved. - Expected User to have a has_one association called avatar_blob (avatar_blob should resolve to ActiveStorage::Blob for class_name) + Expected User to have a has_one association called avatar_blob through avatar_attachment (avatar_blob should resolve to ActiveStorage::Blob for class_name) MESSAGE end end + context 'and the eager loading scope does not exist' do - let(:record) do - record_has_one_attached(:avatar, remove_eager_loading_scope: true) - end it 'matches' do + record = record_having_one_attached(:avatar, remove_eager_loading_scope: true) expect { have_one_attached(:avatar) }. not_to match_against(record). and_fail_with <<-MESSAGE @@ -126,21 +122,18 @@ end context 'when the attached exists on the model' do - let(:record) do - record_has_many_attached(:avatars) - end it 'matches' do + record = record_having_many_attached(:avatars) expect { have_many_attached(:avatars) }. to match_against(record). or_fail_with(<<-MESSAGE) Did not expect User to have a has_many_attached called avatars, but it does. MESSAGE end + context 'and the reader attribute does not exist' do - let(:record) do - record_has_many_attached(:avatars, remove_reader: true) - end it 'matches' do + record = record_having_many_attached(:avatars, remove_reader: true) expect { have_many_attached(:avatars) }. not_to match_against(record). and_fail_with(<<-MESSAGE) @@ -149,11 +142,10 @@ MESSAGE end end + context 'and the writer attribute does not exist' do - let(:record) do - record_has_many_attached(:avatars, remove_writer: true) - end it 'matches' do + record = record_having_many_attached(:avatars, remove_writer: true) expect { have_many_attached(:avatars) }. not_to match_against(record). and_fail_with(<<-MESSAGE) @@ -162,11 +154,10 @@ MESSAGE end end + context 'and the attachments association does not exist' do - let(:record) do - record_has_many_attached(:avatars, remove_attachments: true) - end it 'matches' do + record = record_having_many_attached(:avatars, remove_attachments: true) expect { have_many_attached(:avatars) }. not_to match_against(record). and_fail_with(<<-MESSAGE) @@ -175,25 +166,22 @@ MESSAGE end end + context 'and the blobs association is invalid' do - let(:record) do - record_has_many_attached(:avatars, invalidate_blobs: true) - end it 'matches' do + record = record_having_many_attached(:avatars, invalidate_blobs: true) expect { have_many_attached(:avatars) }. not_to match_against(record). and_fail_with(<<-MESSAGE) Expected User to have a has_many_attached called avatars, but this could not be proved. - Expected User to have a has_many association called avatars_blobs (avatars_blobs should resolve to ActiveStorage::Blob for class_name) + Expected User to have a has_many association called avatars_blobs through avatars_attachments (avatars_blobs should resolve to ActiveStorage::Blob for class_name) MESSAGE end end context 'and the eager loading scope does not exist' do - let(:record) do - record_has_many_attached(:avatars, remove_eager_loading_scope: true) - end it 'matches' do + record = record_having_many_attached(:avatars, remove_eager_loading_scope: true) expect { have_many_attached(:avatars) }. not_to match_against(record). and_fail_with(<<-MESSAGE) @@ -207,7 +195,7 @@ end end -def record_has_one_attached( +def record_having_one_attached( attached_name, model_name: 'User', remove_reader: false, @@ -245,7 +233,7 @@ def record_has_one_attached( model.new end -def record_has_many_attached( +def record_having_many_attached( attached_name, model_name: 'User', remove_reader: false,