How to upload a file in a Rails Rspec request spec

前端 未结 5 2192
没有蜡笔的小新
没有蜡笔的小新 2021-02-19 03:20

I want to test uploading a file in a Rails Rspec request test. I\'m using Paperclip for asset storage.

I\'ve tried:

path = \'path/to/fixture_file\'
para         


        
5条回答
  •  再見小時候
    2021-02-19 03:25

    In rails_helper.rb do

    include ActionDispatch::TestProcess
    include Rack::Test::Methods
    

    Then you have few options. 1. You can use fixture_file_upload helper

      let(:file) { fixture_file_upload('files/image.jpg') }
      it 'it should work' do
        post some_path, params: { uploads: { file: file } }
      end
    
    1. You can use Uploaded file but you have to give full path
      let(:file) { Rack::Test::UploadedFile.new(Rails.root.join('spec',
      'fixtures', 'blank.jpg'), 'image/jpg') }
      let(:params) { { attachment: file, variant_ids: ['', product.master.id] } }
      let(:action) { post :create, product_id: product.slug, image: params }
    

提交回复
热议问题