Testing ActiveStorage Uploads in Rails 5.2
This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.
With ActiveStorage it's pretty simple to test your controllers and check that file uploads are working the way you expect. Just configure your disk for the test environment and submit a fixture as part of the request and you're good to go.
First configure your test environment to use ActiveStorage's test disk. You can do this in
Once that's done you can use
fixture_file_upload to get an image object you can submit with your request. In this example I'm using an image that's in my public folder, but you can use whatever file you like.
By checking that an
ActiveStorage::Attachment model was created we can be sure that the file was accepted and uploaded as expected. If you're curious to see what the controller code for this example might look like, take a look at the following code.