Want more GoRails?

GoRails is packed full with 684 lessons just like this one.

Sign up to get full access or log in to your account and sit back.

Track your progress

Sign in to track your progress and access subscription-only lessons.

Log In

Your Teacher

Hi, I'm Chris. I'm the creator of GoRails, Hatchbox.io and Jumpstart. I spend my time creating tutorials and tools to help Ruby on Rails developers build apps better and faster.

About This Episode

Learn how to test ActiveStorage file uploads and create fixtures with ActiveStorage attachments

Notes

Testing file uploads can seem daunting, but they're really not too bad. ActiveStorage provides two database tables for associating file uploads with any model.

We can create fixtures for Active Storage file uploads and associate them with any model in our database. This makes it easy to write tests for records that should have file uploads associated with them.

For uploading files, Rails provides some helpers for loading a file for uploading so you can pass it directly to your controller as a param. This works the exact same way as it does in the browser using a Rack Uploaded File object.

The two main things you need to test ActiveStorage file uploads is use the fixture_file_upload helper and create fixtures in test/fixtures/active_storage/blobs.yml and test/fixtures/active_storage/attachments.yml

Resources

Want to stay up-to-date with Ruby on Rails?

Join 81,536+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.