Uploading files with Capybara in RSpec tests

You can easily upload files with Capybara when testing with RSpec

Capybara is a great tool for testing your Rails app. Recently I had cause to test uploading files through a particular UI. Here is how you go about in Capybara.

attach_file("Upload Your File", Rails.root + "spec/fixtures/file.pdf")

What this tells Capybara is look for a file upload input with the label “Upload Your File” and then inserts the file specified.

I found I needed the Rails.root for the file to upload correctly.

In order to tidy up your tests I would recommend making a helper method called something like pdf_upload_file which would return Rails.root + "spec/fixtures/file.pdf"

Recent posts View all


Upgrading Ruby - Steps to follow

Here are the steps you should follow to upgrade Ruby as stress free as possible

Team Management

Meeting Agendas (and why you should have them)

Please, please, please add meeting agendas to your meeting invites