Using FactoryGirl_for attributes with clip attachments

I have a model called Photothat has an inscription paper clip attachment image.

I have working specifications that test the creation of a new photo with an attachment and can create them manually.

I have the following FactoryGirl factory used in all my specifications:

FactoryGirl.define do
 factory :photo do
    image { File.new(File.join(Rails.root, 'spec', 'fixtures', 'images', 'minimum.jpg')) }
    # Other attributes omitted
 end
end

I need to use attributes_for(:photo)to generate the attributes that need to be passed to the PhotoController create action, but at the same time Paperclip throws an error:

Paperclip::AdapterRegistry::NoHandlerError:
       No handler found for "#<File:0x007f87c0a1d980>"

I see that if I create a new photo using a browser, the attribute imagelooks like this:

"image"=>#<ActionDispatch::Http::UploadedFile:0x007fbc480b1c18 @tempfile=#<Tempfile:/var/folders/bv/x495g9g10m7119680c9ssqmr0000gn/T/RackMultipart20140622-45603-a1h9a8>, @original_filename="equals_cover_art_old.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"equals_cover_art_old.jpg\"\r\nContent-Type: image/jpeg\r\n">}

However, the image attribute generated attributes_for(:photo)is as follows:

:image=>#<File:/Users/me/Documents/Work/Websites/example/spec/fixtures/images/minimum.jpg>

attributes_for(:photo) ?

+4
2

image Rack:: Test:: UploadedFile File.

FactoryGirl.define do
  factory :photo do
    image Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/images/minimum.jpg", "image/jpg")
    # Other attributes
  end
end
+7

ActionDispatch::TestProcess:

include ActionDispatch::TestProcess

FactoryGirl.define do
  factory :photo do
    image { fixture_file_upload( File.join(Rails.root, 'spec', 'fixtures', 'images', 'minimum.jpg'), 'image/jpeg') }
  end
end
+2

Source: https://habr.com/ru/post/1545591/


All Articles