Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload a file in a Rails Rspec request spec

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'
params = { file: Rack::Test::UploadedFile.new(path, 'application/pdf', true) }
post v1_product_documents_path, params: params

In the controller, I get a string

"#Rack::Test::UploadedFile:0x0055b544479128>"

instead of the actual file.

The same code works in controller tests

like image 293
hirowatari Avatar asked Nov 25 '16 00:11

hirowatari


1 Answers

try to use fixture_file_upload: fixture_file_upload

or if you wanted to use this in a factory

 Rack::Test::UploadedFile.new(File.open(File.join(Rails.root, '/spec/fixtures/images/bob-weir.jpg')))
like image 174
vipin Avatar answered Sep 20 '22 12:09

vipin