3

人们如何处理以下情况:我有一个拍摄图像的表格。如何验证图像是否被接受?或者这不是你应该在这个级别的测试中验证的东西吗?

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

有没有一种特殊的方法来验证它attach_file实际上是否成功?此外,人们一般如何测试文件上传?在您的请求规格、控制器规格和型号规格中?我想坚持使用 RSpec 和 Capybara 进行所有测试。

谢谢

4

1 回答 1

3

既然attach_file是水豚方法,就不要测试了。

相反,请考虑有效/无效上传如何改变您的系统状态。例如,如果上传用户头像,请验证用户模型是否有指向您刚刚上传的图像 ID 的指针。如果您正在进行更高级别的测试,请验证用户是否看到了他们希望在页面上看到的上传图片(网址)。

总之,不要测试图片上传,测试它的结果。

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

于 2013-10-31T22:58:54.847 回答