Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails Capybara::Ambiguous: Ambiguous match

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.

like image 398
jwong Avatar asked Feb 07 '26 17:02

jwong


2 Answers

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.

like image 183
Jimmy Avatar answered Feb 09 '26 10:02

Jimmy


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

like image 33
Ben Avatar answered Feb 09 '26 11:02

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!