Skip to content

Commit

Permalink
Remove inconsistencies from active storage matcher specs
Browse files Browse the repository at this point in the history
  • Loading branch information
StefSchenkelaars committed Jul 13, 2020
1 parent 9132eb3 commit 04151ef
Showing 1 changed file with 29 additions and 41 deletions.
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -207,7 +195,7 @@
end
end

def record_has_one_attached(
def record_having_one_attached(
attached_name,
model_name: 'User',
remove_reader: false,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 04151ef

Please sign in to comment.