Skip to main content

Join GoRails to continue learning

Subscribe to GoRails to get access to this episode and all other pro episodes, and access to our private Slack community of Ruby and Rails devs.

Subscribe to watch

Login to your account


Testing ActiveStorage File Uploads and Fixtures


Episode 332 · February 17, 2020

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

ActiveStorage File Uploading Testing

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



Earn a free month


Subscribe to the newsletter

Join 31,152+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.