我想做图片上传测试(功能规范)
I want to do Image upload test(feature spec)
我想测试图片上传器。
我的错误是
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)
<%= form_with(model: post, local: true) do |f| %>
<div class="mb-3">
<%= f.label :picture %>
<%= f.file_field :picture %>
</div>
<% end %>
更新:(posts/new.html)
<form action="/posts" method="post">
<input type="hidden" ... />
<div class="mb-3">
<label for="post_picture">Picture</label>
<input type="file" name="post[picture]" id="post_picture" />
</div>
</form>
我能够匹配 post[picture]
的名称(请参阅下面的答案),但同样 "Picture" 的文本和 post_picture
的 ID 的其他建议也有效。
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 文件输入的样式。如果您的文件输入实际上被设置为不可见的样式,然后用某种按钮或图像替换,以使其看起来更好,那么您将需要使用可以传递给 [=12] 的 make_visible
选项=].这将临时调整 CSS 以使输入可见 - 附加文件 - 然后将 CSS 重置为原始状态。像
attach_file 'post_picture', "#{Rails.root}/spec/files/attachment.jpg", make_visible: true
我想测试图片上传器。
我的错误是
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)
<%= form_with(model: post, local: true) do |f| %>
<div class="mb-3">
<%= f.label :picture %>
<%= f.file_field :picture %>
</div>
<% end %>
更新:(posts/new.html)
<form action="/posts" method="post">
<input type="hidden" ... />
<div class="mb-3">
<label for="post_picture">Picture</label>
<input type="file" name="post[picture]" id="post_picture" />
</div>
</form>
我能够匹配 post[picture]
的名称(请参阅下面的答案),但同样 "Picture" 的文本和 post_picture
的 ID 的其他建议也有效。
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 文件输入的样式。如果您的文件输入实际上被设置为不可见的样式,然后用某种按钮或图像替换,以使其看起来更好,那么您将需要使用可以传递给 [=12] 的 make_visible
选项=].这将临时调整 CSS 以使输入可见 - 附加文件 - 然后将 CSS 重置为原始状态。像
attach_file 'post_picture', "#{Rails.root}/spec/files/attachment.jpg", make_visible: true