Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara attach_file

I have a feature test in my application where I test uploading a file. So I have something like this:

attach_file :input_id, Rails.root + "spec/fixtures/sample.jpg"
click_button "Upload"

My "expects" are always failing, I add a binding.pry to my controller to check why. It turns out the what is being passed to the input file is a string.

Has anyone encountered the same problem? Any suggestions on how to go about the problem? Been stuck at this problem for 2 days now.


EDIT

Forgot to mention, the input file is a multiple type

f.attachment_field :input_field, multiple: true
like image 245
cubeguerrero Avatar asked Jan 19 '16 05:01

cubeguerrero


2 Answers

I asked a question in the comment about whether #attachment_field is from refile and you're using direct or presigned mode. To save time if your answers to that are yes:

Refile when using direct or presigned mode doesn't upload the file with the form, rather it submits the file using ajax to its own sinatra app, S3, etc (dependent on the backend used) and then submits the id of the file upload in the hidden field added to the form. In that case you would expect that to be a string. You can read more about it https://github.com/refile/refile#4-rails-helpers and https://github.com/refile/refile#5-javascript-library

like image 100
Thomas Walpole Avatar answered Nov 11 '22 04:11

Thomas Walpole


try this:

attach_file('Image', "spec/files/images/yourtestimg.jpg")

Create those folders in the path, obviously. Sorry, I'd rather over explain than not. yourtestimg.jpg is referring to an actual image i put in that folder.

A quick side note, the above is the correct way to do it, however one time I ran into an fail because it didn't recognise 'Image'. In that case, right click and inspect element of the upload button and see what the field has been named. In the fail I am talking about the field was name 'post_image', likewise the caption field was named 'post_caption'. Once I swapped Image for 'post_image' it passed fine.

Justin

like image 26
RuNpiXelruN Avatar answered Nov 11 '22 04:11

RuNpiXelruN