Reputation: 358
I'm very new to rails and still learning it. Now I got the test failure message like this:
1) LayoutLinks should have the right links on the layout
Failure/Error: click_link("Help")
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching link "Help"
# ./spec/requests/layout_links_spec.rb:47:in `block (2 levels) in <top (required)>'
because I have two identical "help" links on my homepage but it seems the test thinks it's an ambiguous match and I want it to test both of them the same way because the links direct to the same page.
Actually there are several solutions on the site but most of them are pretty complicated to me since I'm very new, so can anyone give me the simpliest solution to it? Thanks in advance.
Updated:
There's nothing complicated in my view and spec. I'm still learning everything.
Here's my spec
visit root_path
expect(page).to have_title("Home")
click_link("About")
expect(page).to have_title("About")
click_link("Contact")
expect(page).to have_title("Contact")
click_link("Help")
expect(page).to have_title("Help")
Here's my header partial in the layout
<header>
<nav class="round">
<ul>
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<li><%= link_to "Sign in", "#" %></li>
</ul>
</nav>
</header>
And here's my footer partial in the layout
<footer>
<nav class="round">
<ul>
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Contact", contact_path %></li>
<li><%= link_to "Help", help_path %></li>
</ul>
</nav>
</footer>
Note that there are two sepearte help links in the two partial, which are identical.
Upvotes: 2
Views: 6020
Reputation: 126
You might want to try either one of these
click_link("Help", :match => :first)
click_link("Help", :match => :smart)
click_link("Help", :match => :prefer_exact)
click_link("Help", :match => :one)
Hope this helps.
Upvotes: 7
Reputation: 168
On way to do it is by making the spec more specific by clarifying which help button to click by naming the surrounding element. This only works if they're within different elements
based on the html you posted ... I'd replace the click_link("Help) with one of the following depending on what link you would like to test. (In the header or footer)
within("header") do
click_link "Help"
end
or
within("footer") do
click_link "Help"
end
or check out this solution - > Capybara Ambiguity Resolution
Upvotes: 5