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 Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Editing Todo Lists

Andrew Carr
Andrew Carr
10,979 Points

Unexpected Error with CSS not finding #todo_list

Hello. My code matches the instructor's I'm 90% sure, but I'm getting an error regarding the test not being able to identify the list:

Failures:

  1) Editing todo lists updates a todo listsuccessfully with correct info
     Failure/Error: within "#todo_list_#{todo_list.id}" do
     NameError:
       undefined local variable or method `todo_list' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fa5ccfe5d98>
     # ./spec/features/todo_lists/edit_spec.rb:8:in `update_todo_list'
     # ./spec/features/todo_lists/edit_spec.rb:19:in `block (2 levels) in <top (required)>'

Finished in 0.0534 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/todo_lists/edit_spec.rb:17 # Editing todo lists updates a todo listsuccessfully with correct info

Granted I sort of understand, since i don't get how even with adding the DOM ID the test code would be able to specify which one of the edit signs I should use. My code is as such:

require 'spec_helper'

describe "Editing todo lists" do 
    let!(:todo_list) {TodoList.create(title: "Groceries", description: "Organic things")}


    def update_todo_list(options={})
        options[:title] ||= "My todo list"
        options[:description] ||="This is my todo list"

        todo_list = options[:todo_list]

        within "#todo_list_#{todo_list.id}" do 
        click_link "Edit"
    end

        fill_in "Title", with: opttions[:title]
        fill_in "Description", with: options[:description]
        click_button "Update Todo list"
    end

it "updates a todo listsuccessfully with correct info" do 
        update_todo_list todo_list: todo_list, 
                         title: "New Title", 
                         description: "New Description"

        todo_list.reload

        expect(page).to have_content("Todo list was successfully updated.")
        expect(todo_list.title).to eq("New title")
        expect(todo_list.description). to eq("New Description")
    end
end

Many thanks!

5 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You have to visit a url before you start a feature test. Try adding this at the beginning of your "updates a todo listsuccessfully with correct info" test:

visit todo_lists_path
Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You can also make it a part of the update tets helper above. Either way, right now the tests are not performed on anything. Jason has this line "visit '/todo_lists' - that's the part you need.

Brandon Barrette
Brandon Barrette
20,485 Points

The only thing I can think of is have you added the dom_id to the HTML? I've compared your code to my code and can't spot any error. I would also restart your server, and then visit this page to see if any errors crop up. Something small is causing it. If you do get any errors, please paste them here, it will help with the diagnosis.

Finn Terdal
Finn Terdal
15,997 Points

I'm encountering the same problem. When I've inspected each tr element, I see that it starts at #todo_list_4, rather than #todo_list_1. My edit_todo_list test fails because it says it cannot find the css element #todo_list_1.

I'm pretty sure I added all the correct HTML to the index.html.erb file. Here's my HTML code:

<tr id="<%= dom_id(todo_list) %>">

And here's the rspec test:

visit "/todo_lists"
todo_list = TodoList.create(name: "Groceries", description: "List of Groceries.")
within "#todo_list_#{todo_list.id}" do
    click_link "Edit"
end

And here's the error I get: Failure/Error: within "#todo_list_#{todo_list.id}" do Capybara::ElementNotFound: Unable to find css "#todo_list_1"

Any assistance would be great!

I'm at exactly the same point as Finn Terdal - my id don't seem to have started from 0 or 1 so my test code fails with the same error, Unable to find css "#todo_list_2".

I don't know anywhere near enough to know where to look for this. However, the index is created in the index.html.erb file by adding a dom_id to each table row. My code for this looks like,

<tr id="<%= dom_id(todo_list) %>">

This id is then used in the update_todo_list method inside edit_spec.rb which looks like:

    def update_todo_list(options={})
        options[:title] ||= "My todo list"
        options[:description] ||= "This is my todo list."

        todo_list = options[:todo_list]

        within "#todo_list_#{todo_list.id}" do
            click_link "Edit"
        end
    end

Who to ask for help? Well, if Jason Seifer is around, he should be able to assist, else, I'd bet that Maciej Czuchnowski will be able to point us in the right direction!

I hope so!

Steve.

If it helps, I've added a new branch to my Github repo called 'unresolved' that includes all the code with the error in it. The repo is here.

Steve.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Steve, a pull request is waiting for you. Some of the changes are just my text editor's default behavior of removing trailing spaces. Your main problem was that in the test you did not visit any particular path, so the tests were performed on...nothing :). I took the liberty of fixing all the tests, also the controller tests. Make sure you read through the changes and let me know if you understand them. You can reach me directly by email (on GitHub), it's more likely I will respond.

Thanks for sorting all that out, Maciej - it is all working fine now. :-)

Steve.