To achieve a similar setup with Shrine using the filesystem storage, you can upload the file to the storage in setup, and clear the storage in teardown. Then you can assign the uploaded file to the <attachment>_data column in the fixture.
# test/test_helper.rb
class ActiveSupport::TestCase
setup do
image = File.open("test/fixtures/files/tapir.jpg")
Shrine.storages[:store].upload(image, "tapir.jpg")
end
teardown do
Shrine.storages.values.each(&:clear!)
end
end
# test/fixtures/users.yml
user_with_avatar:
avatar_data: '{"id":"tapir.jpg","storage":"store","metadata":{"filename":"tapir.jpg","mime_type":"image/jpeg","size":98423}}'
With Shrine you can also use shrine-memory storage, so that you don't have to worry about cleaning the directory (and it's also faster than writing to disk).
# Gemfile
gem "shrine-memory"
# test/test_helper.rb
require "shrine/storage/memory"
Shrine.storages = {
cache: Shrine::Storage::Memory.new,
store: Shrine::Storage::Memory.new,
}
class ActiveSupport::TestCase
setup do
image = File.open("test/fixtures/files/tapir.jpg")
Shrine.storages[:store].upload(image, "tapir.jpg")
end
end
# test/fixtures/users.yml (same as above)
user_with_avatar:
avatar_data: '{"id":"tapir.jpg","storage":"store","metadata":{"filename":"tapir.jpg","mime_type":"image/jpeg","size":98423}}'
Note that usage questions are best asked on the Shrine google group, GitHub issues should be used here only for bug reports 馃槈
To follow-up here as well, I realized that probably the easiest way to attach files in fixtures is using data URIs, I explained it here.
Massive thanks for this info @janko-m, much appreciated!
@pmackay Unfortunately, this strategy doesn't work, because Rails fixtures seem to require that all key-value pairs are actual columns, it won't accept virtual attributes (like <attachment>_data_uri). See https://github.com/janko-m/shrine/issues/117 for a discussion about this.
With this limitation I didn't see any correct way to setup attachment with Rails fixtures, so I removed the "fixtures" section from the "Testing Shrine" guide.
Most helpful comment
To achieve a similar setup with Shrine using the filesystem storage, you can upload the file to the storage in
setup, and clear the storage inteardown. Then you can assign the uploaded file to the<attachment>_datacolumn in the fixture.With Shrine you can also use shrine-memory storage, so that you don't have to worry about cleaning the directory (and it's also faster than writing to disk).
Note that usage questions are best asked on the Shrine google group, GitHub issues should be used here only for bug reports 馃槈