All Lessons / Testing ActiveStorage File Uploads and Fixtures

Want more GoRails?

GoRails is packed full with over 350 lessons just like this one.

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

Testing ActiveStorage File Uploads and Fixtures

#332 ยท February 17, 2020

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

Resources

Discussion

Loading...
logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.