rspec - Rails 4 Paperclip FactoryGirl file uploading -
i have factorygirl :product factory uses fixture_file_upload
set image
, paperclip attachment.
image { fixture_file_upload "#{rails.root}/spec/fixtures/images/product.png", 'image/png' }
fixture_file_upload
works fine, but every time test creates new product using factory, paperclip creates new file in publicproducts/<id>/original.png
. this issue.. filling folder publicproducts
on each test run not acceptable.
the first workaround can think of solution mentioned in https://github.com/carrierwaveuploader/carrierwave/wiki/how-to:-cleanup-after-your-rspec-tests
have solved problem in way?
the solution, mentioned deep to:
- specify paperclip in test environment should upload files folder
test_uploads
, - modify factory_girl factories upload fixture ex.
spec/fixtures/images/filename.extension
, - add after cleanup block in
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) if rails.env.test? test_uploads = dir["#{rails.root}/test_uploads"] fileutils.rm_rf(test_uploads) end end ...
Comments
Post a Comment