Capybara doesn't attach file

Hi,

I’m trying to pass a feature test for uploading an image.

my form looks like this:

<%= form_for [:admin, @research_project] do |f| %>
  <div>
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div>
    <%= f.label :body %>
    <%= f.text_area :body %>
  </div>
  <div>
    <%= f.label :image %>
     <%= f.file_field :image %>
  </div>
   <div>
     <%= f.collection_check_boxes :research_theme_ids, ResearchTheme.all, :id, :title %>
  </div>
  <% if @research_project.new_record? %>
     <%= f.submit "Add Research Project" %>
  <% else %>
    <%= f.submit "Update Research Project" %>
  <% end %>
<% end %>

my feature test looks like this:

  scenario 'Admin adds a research project' do
    expect{
    click_link "Add Research Project"
    fill_in 'Title', with: @research_project.title 
    fill_in 'Body', with: @research_project.body 
    attach_file 'Image', "spec/support/uploads/monk_large.jpg"
    click_button "Add Research Project"
    }.to change(ResearchProject, :count).by(1)

    expect(page).to have_content @research_project.title 
    expect(page).to have_content "you successfully added a new research project"

    visit admin_research_project_path @research_project.id
    expect(page).to have_css 'img', text: "monk_large.jpg"
  end

when I run the test, I get this failure message, indication there is an img element but with no value for the src attribute:

1) Admin interacts with research projects Admin adds a research project
     Failure/Error: expect(page).to have_css 'img', text: "monk_large.jpg"
     Capybara::ExpectationNotMet:
       expected to find css "img" with text "monk_large.jpg" but there were no matches. Also found "", which      matched the selector but not all filters.

Can’t figure out why Capybara won’t attach the file. I put the test image in the folder: /spec/support/uploads

Any help is more then welcome

my repo is at: https://github.com/acandael/posplus-rails

greetings,

Anthony

Shouldn’t it be src: "monk_large.jpg instead of text: "monk_large.jpg" ?

Hi Andy,

I tried you’re suggestion but apperently :src is not a supported key:

Failure/Error: expect(page).to have_css 'img', src: "monk_large.jpg"
 ArgumentError:
   invalid keys :src, should be one of :text, :visible, :between, :count, :maximum, :minimum, :exact, :match, :wait

thanks for your help anyway,

Anthony

Try this instead:

expect(page).to have_css("img[src='monk_large.jpg']")

no, doesn’t work. When I check the page with the Launch command ‘save_and_open_page’, there is no image on the page. I don’t understand this, because if I do a ‘sanity test’, the image is there.

In another project, I used this feature test for testing image uploads, and it worked fine:

feature 'Admin adds new video' do
  scenario 'Admin successfully adds a new video' do
    admin = Fabricate(:admin)
    dramas = Fabricate(:genre, name: "Dramas")
    sign_in(admin)
    visit new_admin_video_path

    fill_in "Title", with: "Monk"
    select "Dramas", from: "Genre"
    fill_in "Description", with: "SF detective"
    attach_file "Large cover", "spec/support/uploads/monk_large.jpg"
    attach_file "Small cover", "spec/support/uploads/monk.jpg"
    fill_in "Video URL", with: "http://www.example.com/my_video.mp4"

    click_button "Add Video"

    sign_out
    sign_in

    visit video_path(Video.first)
    expect(page).to have_selector("img[src='/my-flix/monk_large.jpg']")
    expect(page).to have_selector("a[href='http://www.example.com/my_video.mp4']")
  end
end

for the project I’m currently working on, I’m doing exactly the same, but the test fails.

So at the moment I don’t know if I’m missing a Capybara configuration somewhere, or if something has changed in Capybare 2.3.0

And does it work for you in the browser? I mean can you attach file to this form?

yes, in the browser everything works fine, I’m able to select an image and upload it.