I have implemented a JQuery-file-upload in my Rails4 app. File upload works when I manually test it from the browser, but my test for it fails.
Below is my spec for the JQuery-file-upload: require 'spec_helper'
feature 'Evidences' do
context "as an assessor user" do
let!(:assessor) { User.make! :assessor }
let!(:assessment) { Assessment.make! }
background { sign_in assessor }
scenario "it uploads evidence", js: true do
evidences_count_before_upload = assessment.evidences.count
visit edit_assessment_path(assessment)
path = "#{Rails.root}/spec/fixtures/files/sample1.doc"
attach_file 'evidence_file_url', path
expect(assessment.evidences.count).to eq(evidences_count_before_upload + 1)
end
end
end
I'm using RSpec 2, Capybara 2 and Poltergeist for this feature spec.
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. What this tells Capybara is look for a file upload input with the label “Upload Your File” and then inserts the file specified.
Capybara is an exceptionally great tool for performing an integration test with RSpec because it helps you perform end-to-end tests on your applications. It does this by simulating how a real user would interact with your application.
RSpec gives us helpers for simulating requests, attributes for accessing values that are being assembled by the controller, and additional matchers for setting expectations on responses.
For example, when a user visits a home page, gets redirected to sign in, fills the login form and clicks the login button. I'll often refer to Request Specs as a Controller Spec since they both test the actions in the controller. Have you noticed that if you try to generate a controller spec in your application, it creates a request spec instead?
I am also using JQuery-file-upload with RSpec and Capybara. I am using the capybara-webkit driver, but this should work with selenium as well.
See the example method and usage found in this answer: https://stackoverflow.com/a/11203629/1084109
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With