Rails 4 Paperclip FactoryGirl File Downloads

I have a FactoryGirl: a factory product that it uses fixture_file_upload

to install image

, which is an attachment to a Paperclip.

    image { fixture_file_upload "#{Rails.root}/spec/fixtures/images/product.png", 'image/png' }

      

fixture_file_upload

works fine, but every time the test creates a new product using the factory, Paperclip creates a new file in publicproducts/<id>/original.png

. This is problem. ... Filling the folder publicproducts

in every test run is not acceptable.

The first workaround I can think of is the solution mentioned at https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Cleanup-after-your-Rspec-tests

Did you solve this problem differently?

+3


source to share


1 answer


The solution, also mentioned by Deep, is as follows:

  • specify that a paperclip in a test environment should upload files to a folder test_uploads

    ,
  • Modify factory_girl to load fixture from ex. spec/fixtures/images/filename.extension

    ,
  • add block after cleanup to 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
  ...

      

+11


source







All Articles