RSpec /水豚找不到图片的链接

时间:2018-12-30 12:40:57

标签: ruby-on-rails ruby rspec capybara

我想使用RSpec和Capybara模拟图像点击,但是我无法做到。

我的错误是这样的:

Failure/Error: click_on "icon_red_heart.png"

     Capybara::ElementNotFound:
       Unable to find visible css "#image-button"

spec / features / 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 / _likes.html.erb

   <%= button_to post_like_path(post, like), id: "image-button",method: :delete, remote: true do %>

      <%= image_tag("icon_red_heart.png")%>

我不知道如何指定图像。

请教我。

1 个答案:

答案 0 :(得分:1)

如@jonrsharpe所示,您未正确引用该按钮。引用元素的最灵活的方法是为其指定一个ID并通过该ID对其进行引用。

此外,由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,这大概是您想要的。

相关问题