Episode 332 · February 17, 2020
Learn how to test ActiveStorage file uploads and create fixtures with ActiveStorage attachments
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
Join 31,152+ developers who get early access to new screencasts, articles, guides, updates, and more.