Rails 使用 RSpec 和 Capybara 的测试无法在 application.html.erb 中找到链接

Rails Tests with RSpec and Capybara can't find links in application.html.erb

我正在使用 RSpec 和 Capybara 为我的 Rails 应用程序编写功能测试,但我的测试失败并显示错误消息:

Failure/Error: click_link "Google"

     Capybara::ElementNotFound:
       Unable to find visible link "Google"

我的规范写成:

 it "lists logged in user's profile data" do
      visit '/'
      mock_auth_hash
      click_link "Google"
      click_link "User"
      expect(page).to have_content('mockuser')
    end 

我的 Google 登录名 link_to 在我的 application.html.erb 布局中:

<a class="nav-item nav-link"><%= link_to 'Google', '/auth/google' %></a>

当我将相同的 link_to 放入我的根页面时,测试顺利通过。水豚似乎很可能没有在我的 application.html.erb 布局中选择 HTML。不过我不确定如何解决这个问题,而且 Capybara 的文档没有说明任何解决方案。

感谢您提供的任何帮助。

Capybara 不太可能只是忽略了您页面的某些部分,更有可能是您的应用在呈现您的页面时实际上没有正确呈现该布局(可能是因为您正在呈现 link在 link 中 - 这是非法的 html https://www.w3.org/TR/html52/textlevel-semantics.html#the-a-element - 出于某种原因??)。在 visit 之后休眠几秒钟,然后如果您使用的驱动程序支持屏幕截图 - 如果不使用 save_and_open_page 并检查 HTML 以查看实际内容页。

visit('/')
sleep 3
save_and_open_screenshot # If the driver supports screenshots
# save_and_open_page # To look at the actual HTML

很可能您会发现页面上确实没有 link。