Attaching Fixture Files in Rails Model Tests
If you want to attach fixture files in a model test. Assuming your ActiveStorage association is already set up, if it's not set up, check this out, then follow these steps:
-
Make sure your desired fixture file has been placed in your
test/fixtures/files
folder -
Attach the fixture to the model instance by providing the
.attach
method with a hash including an IO object and the name of the file you wish to attach.
It should look something like this:
@object.image.attach(io: File.open('test/fixtures/files/filename'), filename: 'filename')
Tweet