Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

having trouble testing upload file with Capybara attach_file method

I'm having trouble trying to test whether image upload works with paperclip using Capybara's attach_file method.

rspec returns these errors below: Failures:

1) Upload Background Image cannot upload a background image
 Failure/Error: page.attach_file('#artist_background_image', Rails.root +         'spec/Fixtures/Snow.jpg')
 Capybara::ElementNotFound:
   Unable to find file field "#artist_background_image"
 # ./spec/models/artist_spec.rb:65:in `block (2 levels) in <top (required)>'

my test is listed below. I assumed the choose file button is the selector for attach_file so I used the id of the selector '#artist_background_page'.

describe 'Upload Background Image', js: true do
  before :each do
    p '================='
    pp Artist.all
    @artist = Artist.first || Artist.create(artist_name:'Double Stuff Oreos', email:'[email protected]', password:'letmein')
    visit "/artists/" + @artist.id.to_s
    pp @artist.id
    pp @artist.artist_name
    p @artist.errors.full_messages
    click_button 'Log in'
    fill_in 'Email', with: '[email protected]'
    fill_in 'Password', with: 'letmein'
    page.find('#loginButtonModal').click
    page.find('#addBackgroundImagePrompt').click
    page.attach_file('#artist_background_image', Rails.root + 'spec/Fixtures/Snow.jpg')
    p Rails.root + 'spec/Fixtures/Snow.jpg'
    click_button 'upload'
  end

  it "cannot upload a background image", js: true do
    backgroundURL = @artist.background_image.url.include?('Snow.jpg')
    p @artist.background_image.url
    expect(backgroundURL).to be_truthy
  end
end

However when I change the selector to just 'artist_background_page' without the id '#' symbol. rspec gives me different error:

...."================="
[]
"is_active_session is called"
#<Artist id: 1, artist_name: "Double Stuff Oreos", route_name: "double-stuff-oreos",       created_at: "2014-07-20 17:20:48", updated_at: "2014-07-20 17:20:48", password_digest:    "$2a$04$vgozdgieklXPjJ9Ri4Cv1e1d/hme0ybNnSEGXrmob5z...", remember_token: nil, email: "[email protected]", description: nil, youtube: nil, twitter: nil, facebook: nil, instagram: nil, hometown: nil, confirmation_code: nil, is_confirmed: nil, background_image_file_name: nil, background_image_content_type: nil, background_image_file_size: nil, background_image_updated_at:   nil>
1
"Double Stuff Oreos"
[]
"is_active_session is called"
#<Pathname:/Users/bob/rails_projects/audience/spec/Fixtures/Snow.jpg>
"/background_images/original/missing.png"
"Hello from update"
"Logged in!"

An error occurred in an after hook
ActiveRecord::RecordNotFound: Couldn't find Artist with 'id'=1
occurred at /Users/shuo/.rvm/gems/ruby-2.1.2/gems/activerecord- 4.1.4/lib/active_record/relation/finder_methods.rb:320:in `raise_record_not_found_exception!'

F....

Failures:

1) Upload Background Image cannot upload a background image
   Failure/Error: expect(backgroundURL).to be_truthy
   expected: truthy value
        got: false
 # ./spec/models/artist_spec.rb:74:in `block (2 levels) in <top (required)>'

In this case, the model was created but the errors says that it cannot find it for some reason... Is there any other method I can use to attach files in capybara and test successfully uploads?

Possibilities for failure:

  1. Wrong path for file upload
  2. Wrong method to use or invalid use of method
  3. something wrong with server
like image 341
riv Avatar asked Jul 20 '14 17:07

riv


1 Answers

This error:

Capybara::ElementNotFound:
   Unable to find file field "#artist_background_image"

shows that capybara can't find a field named #artist_background_image. What you need to do is to reference the file upload field by its name.

Suppose you have

<input id="artist_background_image" name="file_upload" type="file">

Then you can reference the field like:

page.attach_file('file_upload', Rails.root + 'spec/Fixtures/Snow.jpg')

With that, capybara will know what field to upload through.

I know this is a late response but I think it will help other people facing this issue.

like image 62
El'Magnifico Avatar answered Sep 20 '22 16:09

El'Magnifico