我想做图像上传测试(功能规格)

时间:2018-12-30 14:24:59

标签: ruby-on-rails ruby capybara

我要测试图片上传器。

我的错误是

Failure/Error: attach_file "picture", "#{Rails.root}/spec/files/attachment.jpg"

     Capybara::ElementNotFound:
       Unable to find visible file field "picture" that is not disabled

我的代码是 (features / posts_spec.rb)

scenario "Access posting screen and post" do
   sign_in user
    visit new_post_path
    expect(page).to have_current_path(new_post_path)
    fill_in "share impression", with: "hogehoge"
    attach_file "picture", "#{Rails.root}/spec/files/attachment.jpg"
    click_on "post"
    expect(page).to have_current_path(posts_path)
  end

(posts / new.html.erb)

 <div class="mb-3">
    <%= f.label :picture %>
    <%= f.file_field :picture %>
  </div>

请教我一个提示。

1 个答案:

答案 0 :(得分:0)

attach_file通过其ID,名称或相关标签文本来定位文件输入元素,因此picture显然与任何这些都不匹配。如果没有erb模板生成的实际HTML,就无法确切说明您输入的属性是什么,但是根据您的路由名称进行猜测,您可能想要以下内容之一

attach_file 'post_picture', "#{Rails.root}/spec/files/attachment.jpg" # match id of the file input
attach_file "post[picture]", "#{Rails.root}/spec/files/attachment.jpg" # match name of the file input
attach_file "Picture", "#{Rails.root}/spec/files/attachment.jpg" # match associated label text

您可能要处理的另一个潜在问题是文件输入的CSS样式。如果实际上将文件输入的样式设置为不可见,然后用某种按钮或图像替换,以使其看起来更好,那么您将需要使用make_visible选项,该选项可以传递给{{1 }}。这将临时调整CSS以使输入可见-附加文件-然后将CSS重置为原始状态。像

attach_file
相关问题