ruby-on-railsminitestrails-activestorage

Rails 7 ActiveStorage fixture attachment isn't attaching


I'm following the documentation at Active Storage Overview, and trying the answers I've found around here, but no luck.

The assertion in this test always fails:

test "verify attached video" do
  att = sponsor_images(:sponsor_video_1).video_att
  assert att.attached?
end

Here are my relevant files:

config/storage.yml

test_fixtures:
  service: Disk
  root: <%= Rails.root.join("tmp/storage_fixtures") %>

models/sponsor_image.rb

class SponsorImage < ApplicationRecord
  has_one_attached :video_att
  validates :video_att, file_content_type: {
    allow: /^video\/(mp4|m4v)$/,
    if: -> { video_att.attached? }
  }
...

fixtures/sponsor_images.yml

sponsor_video_1:
  sponsor: sponsor_1
  name: Test SponsorImage Video

fixtures/active_storage/attachments.yml

video:
  name: video_att
  record: sponsor_image_1 (SponsorImage)
  blob: video_blob

fixtures/active_storage/blobs.yml

video_blob: <%= ActiveStorage::FixtureSet.blob filename: "1920x1080.mp4", service_name: "test_fixtures" %>

The file fixtures/files/1920x1080.mp4 does exist and is readable.

When I run the test with the debugger, I can see that the attachment exists with the correct data. The file does get created under tmp/storage_fixtures. But the attached? method returns false.

I expect I'm missing something simple, but I sure can't see what it is.


Solution

  • The record identifier in attachments.yml needs to be changed from sponsor_image_1 to sponsor_video_1 to match the object key name in sponsor_images.yml.

    It happens to the best of us, that's why testing is important :)