Want more GoRails?
GoRails is packed full with 725 lessons just like this one.
Sign up to get full access or log in to your account and sit back.
Your Teacher
Chris Oliver
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