E.E.33
E.E.33

Reputation: 2011

Rails: creating a feature spec to test a form that has a file upload field

How are people handling the following situation: I have a form that takes an image. How do I verify whether or not the image was accepted? Or is this not something you should verify at this level of testing?

scenario "add facebook like gate and save as draft" do
  path =  "#{Rails.root}/app/assets/images/like_gate.jpg"

  visit root_url
  click_link I18n.t(:create_a_new_promotion)
  fill_in I18n.t(:title), with: "My promotion"
  click_button I18n.t(:continue)

  expect(page).to have_text(I18n.t(:promotion_details))
  expect(page).to have_text("My promotion")

  fill_in I18n.t(:like_gate_copy), with: "Like our page to enter the contest!"
  attach_file I18n.t(:upload_lg_image), path
  click_button I18n.t(:save_as_draft)

  expect(page).to have_text(I18n.t(:promotion_successfully_saved))
end

Is there a particular way to verify that attach_file was actually successful? Also, how are people testing file uploads in general? In your request specs, controller specs and model specs? I'd like to stick to RSpec and Capybara for all my testing.

Thanks

Upvotes: 3

Views: 1714

Answers (1)

Dane O'Connor
Dane O'Connor

Reputation: 77278

Since attach_file is a capybara method, don't test it.

Instead consider how valid/invalid uploads change your system state. If uploading a user avatar, for example, verify the user model has a pointer to the ID of the image you just uploaded. If you're doing an even higher level test, verify that a user sees the uploaded image (the url) where they'd expect to see it on the page.

In short, don't test image uploading, test the results of it.

expect(page).to have_selector("img[src=#{path}]")

Upvotes: 3

Related Questions