简体   繁体   中英

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

I'm using RSpec and Capybara to write feature tests for my Rails app and the my tests are failing with the error message:

Failure/Error: click_link "Google"

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

My spec is written as:

 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 

My Google login link_to is in my application.html.erb layout:

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

When I take the same link_to and put it into my root page the test passes without a problem. It seems very likely that Capybara is not picking up the HTML in my application.html.erb layout. I am unsure how to fix this though, and Capybara's documentation does not indicate any solutions.

Any help provided is appreciated.

It's unlikely that Capybara is just ignoring some part of your page, and much more likely that your app isn't actually rendering that layout correctly when rendering your page (maybe because you're rendering a link inside a link - which is illegal html https://www.w3.org/TR/html52/textlevel-semantics.html#the-a-element - for some reason??). Sleep for a couple of seconds after the visit and then if the driver you are using supports screenshots - take one, if not use save_and_open_page and inspect the HTML to see what is actually on the page.

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

Odds are you'll find the page really doesn't have that link on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM