Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Capybara :: Ambiguous

I just downgraded from Rspec 3.0 to 2.99, (because that was hell in a hand-basket error-wise), and now I am facing this issue after running

bin/rake This is my failure from the test.

Failures:

  1) Viewing todo items displays the title of the todo list
     Failure/Error: within("h1") do
     Capybara::Ambiguous:
     Ambiguous match, found 2 elements matching css "h1"
     # ./spec/features/todo_items/index_spec.rb:9:in `block (2 levels) in <top (required)>'

Here is my /spec/features/todo_items/index_spec.rb

require 'spec_helper'

describe "Viewing todo items" do
    let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries")}


    it "displays the title of the todo list" do
      visit_todo_list(todo_list)
      within("h1") do
      expect(page).to have_content(todo_list.title)
    end
  end

Here's my views/index.html.erb file:

<h1><%= @todo_list.title %></h1>

<table class="todo_items">
<thead>
    <tr>
        <th width="25%">Completed?</th>
        <th width="65%">Content</th>
        <th width="10%">Functions</th>
    </tr>
</thead>
<tbody>
<% @todo_list.todo_items.each do |todo_item| %>
<tr id="<%= dom_id(todo_item) %>">
    <td>
        <% if todo_item.completed? %>
            <%= todo_item.completed_at %>   
        <% else %>
            <%= link_to "Mark Complete", complete_todo_list_todo_item_path(todo_item), method: :patch %>
        <% end %>
    </td>
    <td>
        <%= todo_item.content %>
    </td>
    <td>
        <%= link_to "Edit", edit_todo_list_todo_item_path(todo_item) %>
        <%= link_to "Delete", todo_list_todo_item_path(todo_item), method: :delete, 
            data: { confirm: "Are you sureeee you want to do that?" } %></td>
</tr>
<% end %>
    </tbody>
</table>

<p>
<%= link_to "New Todo Item", new_todo_list_todo_item_path %>
</p>

Here's my views/todo_lists/show.html.erb

<h1><%= @todo_list.title %></h1>

<h2><%= @todo_list.description %></h2>

<% if @todo_list.has_incomplete_items? %>
<h3>Incomplete Items:</h3>

<ul>
<% @todo_list.todo_items.incomplete.each do |todo_item| %>
<li><%= todo_item.content %></li>
<% end %>
</ul>
<% end %>

<% if @todo_list.has_completed_items? %>
<h3>Complete Items:</h3>

<ul>
<% @todo_list.todo_items.complete.each do |todo_item| %>
<li><%= todo_item.content %></li>
<% end %>
</ul>
<% end %>


<%= link_to 'Edit', edit_todo_list_path(@todo_list) %> |
<%= link_to 'Back', todo_lists_path %>

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

It will be in the views, I'm guessing either todo_items/index.erb or todo_lists/show.erb. Can you paste them both?

Thanks again for all your help Maciej! I owe you big!

I found that if I just comment out the code below it passes. FYI

# it "displays the title of the todo list" do # visit_todo_list(todo_list) # within("h1") do # expect(page).to have_content(todo_list.title) # end # end

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Yeah, but then you don't test that :)

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, what if we remove the whole h1 context? Try making this spec look like this:

require 'spec_helper'

describe "Viewing todo items" do
    let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries")}


    it "displays the title of the todo list" do
      visit_todo_list(todo_list)

      expect(page).to have_content(todo_list.title)

  end

It worked!!!! So now my tests will still run?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

They should. We simply told it in this one test to look for the title on the whole page, not only within h1 tags.