ruby-on-rails - RSpec/Capybara 找不到图像的链接
问题描述
我想使用 RSpec 和 Capybara 来模拟点击图像,但我做不到。
我的错误是这样的:
Failure/Error: click_on "icon_red_heart.png"
Capybara::ElementNotFound:
Unable to find visible css "#image-button"
规格/功能/posts_spec.rb
scenario "push an image" do
visit posts_path
expect(page).to have_current_path(posts_path)
find('#image-button').click
end
喜欢/_likes.html.erb
<%= button_to post_like_path(post, like), id: "image-button",method: :delete, remote: true do %>
<%= image_tag("icon_red_heart.png")%>
我不知道如何指定图像。
请教我。
解决方案
正如@jonrsharpe 指出的那样,您没有正确引用该按钮。引用元素的最有弹性的方法是给它一个 id 并通过该 id 引用它。
此外,由 by 创建的按钮button_to
可能没有内容,在这种情况下,您需要告诉 Capybara 该按钮不可见。
将行更改为button_to
:
<%= button_to post_like_path(post, like), id: "image-button", remote: true do %>
然后将您的测试更改为如下所示:
scenario "push an image" do
visit posts_path # Use the name of your path here
find('#image-button', visible: false).click
end
顺便说一句,method: :delete
在您的button_to
链接中使用并没有达到您的预期。该方法自动设置为POST
,这大概是您想要的。
推荐阅读
- java - 解析 xml 文件时出现异常(3 字节 UTF-8 序列的字节 2 无效)
- python - 寻找一个 dnf 插件来访问 s3 托管的 repos
- c# - 使用 C# 以编程方式在 Xamarin 上编写对象 ID 名称
- javascript - 我想修改这个文本
- javascript - matNoDataRow 显示“未找到数据”,即使数据源(Angular 10.0.8)中有记录使用角度材料在多个选项卡之间切换
- github - 在 PR 合并后,有没有办法在另一个 repo 中的 PR 上触发 CircleCI 工作流程?
- sql - 在 PostgreSQL 中如何检索具有特殊字符和字母的列
- javascript - 使用 Javascript 显示每个按钮单击的 HTML 内容
- java - 将 Qute TypeSafe 模板与 Kotlin 一起使用时出现 java.lang.UnsatisfiedLinkError
- c++ - 在 Windows 上使用 _vscwprintf 处理 utf-8 编码字符串时失败