Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test JQuery-file-upload with RSpec and Capybara

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.

like image 994
Gjaldon Avatar asked Aug 23 '13 08:08

Gjaldon


People also ask

How do I test uploading files through a UI in capybara?

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.

What is the best tool for integration testing with RSpec?

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.

What is the use of an RSpec in a controller?

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.

What is an example of a request spec?

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?


1 Answers

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

like image 194
McFadden Avatar answered Sep 23 '22 20:09

McFadden