I have a FactoryGirl :product factory that uses fixture_file_upload
to set image
, which is a Paperclip attachment.
image { fixture_file_upload "#{Rails.root}/spec/fixtures/images/product.png", 'image/png' }
fixture_file_upload
works fine, but every time a test creates a new Product using the factory, Paperclip creates a new file in publicproducts/<id>/original.png
. This is the issue.. Filling a the folder publicproducts
on each test run is not acceptable.
The first workaround I can think of is the solution mentioned in https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Cleanup-after-your-Rspec-tests
Have you solved this problem in another way?
The solution, also mentioned by Deep is to:
test_uploads
,spec/fixtures/images/filename.extension
,rails_helper.rb
In code:
config/environments/test.rb
...
config.paperclip_defaults = {
path: ':rails_root/test_uploads/:class/:id/:attachment/:filename.:extension',
url: ':rails_root/test_uploads/:class/:id/:attachment/:filename.:extension'
}
...
spec/factories/products.rb
image { fixture_file_upload "#{Rails.root}/spec/fixtures/images/product.png", 'image/png' }
rails_helper.rb
...
include ActionDispatch::TestProcess
config.after(:all) do
if Rails.env.test?
test_uploads = Dir["#{Rails.root}/test_uploads"]
FileUtils.rm_rf(test_uploads)
end
end
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With