Using file fixtures

Hi, I’ve been looking for information about how to specify resources in the fixtures/files directory in my fixtures YAML files. For example, if I have the following fixture in products.yml:

one:
  title: MyString
  description: MyText
  image_url: MyString
  price: 9.99

And I want to reference the file image.png in the fixtures/files directory, so that when the test runs that URL and image are available. How can i do that?

Thanks.

You can either use ActiveStorage fixtures with the Rails 6.2.0.alpha main branch (or copy ActiveStorage::FixtureSet + config to your project):

Or you can put a plain file URL in your fixture, something like this (in this case you won’t have access to Active Storage features such as creating variants):

# config/initializers/assets.rb
# add `|| Rails.env.development?` if you plan to run `rails db:fixtures:load` in dev
if Rails.env.test?
  Rails.application.config.assets.paths << "#{Rails.root}/test/fixtures/files/assets"
end
one:
  image_url: /assets/image.jpg
# and put your image in test/fixtures/files/assets/image.jpg
2 Likes

Thanks, I tried the second method and it works well. I’ll look into activestorage fixtures.