How to upload a file in a Rails Rspec request spec

前端 未结 5 2196
没有蜡笔的小新
没有蜡笔的小新 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:41

    Under describe block, include these modules

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

    Now try running the specs

    path = 'path/to/fixture_file'
    params = { "file" => Rack::Test::UploadedFile.new(path, 'application/pdf', true) }
    post v1_product_documents_path, params: params
    

    Also, I guess you forgot to add , between v1_product_documents_path and params: params, please add that and let me know.

    Hope that helps!

提交回复
热议问题