Rails: creating a function specification for form validation that has a file upload field
How do people deal with the following situation: I have a form that takes an image. How can I check if an image has been received? Or is this not something you should test at this testing level?
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 special way to make sure I attach_file
was really successful? Also, how do people test file uploads in general? In the specs of your request, controller specs and model specs? I would like to stick with RSpec and Capybara for all my tests.
thank
source to share
Since it attach_file
is a capybara method, do not test it.
Instead, consider how valid / invalid downloads change the state of your system. For example, if you are uploading a user's avatar, check that the user's model has a pointer to the id of the newly uploaded image. If you're doing an even higher level of testing, make sure the user sees the uploaded image (URL) where they expected to see it on the page.
In short, don't check the image upload, check its results.
expect(page).to have_selector("img[src=#{path}]")
source to share