Reputation: 127
I'm doing an application that needs an upload of images that will later be displayed on another view but so far I haven't found the proper way to test it through a feature using Rspec with Capybara
this is the code a want to test
<%= f.label :photo, "Foto"%>
<%= f.file_field :photo%><br>
as an user seeing the page a I want to simulate the action of adding a photo or a file so it will be displayed like this
<dt>Foto</dt>
<dd><%= image_tag @applicant.photo %></dd>
Upvotes: 0
Views: 1083
Reputation: 49950
When using Capybara you can upload files via file inputs using the attach_file
method - https://rubydoc.info/github/teamcapybara/capybara/Capybara/Node/Actions#attach_file-instance_method
The method takes a locator which is matched against the file input fields id, name, placeholder, associated labels text, etc. There are also a few other more specific options you can pass (see documentation). In your case that means any of
attach_file('applicant_photo', '/path/to/file/to/attach') # id
attach_file('applicant[photo]', '/path/to/file/to/attach') # name attribute
attach_file('Foto', '/path/to/file/to/attach') # associated label text
should work for you. Technically on a page that only has one visible file input field you could also just do
attach_file('/path/to/file/to/attach') # will use the only file input on page
attach_file
also has a block accepting mode for use when the file input field is non-visible and the label is styled to give a more modern UI but that doesn't currently apply to your use case.
Upvotes: 1