Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Editing Todo Lists

Tom Finet
Tom Finet
7,027 Points

None of the tests pass for me. Why?

I have a problem with my ruby on rails application testing. The code I am using is now identical to the code used in the video tutorials. I have no idea what is going on and why this is happening.

Here is my code on the create_spec.rb file:

create_spec.rb
describe "Creating todo lists" do
        def create_todo_list(options={})
            options [:title] ||= "My todo list"
            options [:description] ||= "This is my todo list."

            visit "/todo_lists"
            click_link "New Todo list"
            expect(page).to have_content("New todo_list")

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

        it "redirects to the todo list index page on success" do
            create_todo_list
            expect(page).to have_content("My todo list")
        end

        it "displays an error when the todo list has no title" do
            expect(TodoList.count).to eq(0) 

            create_todo_list title: ""

            expect(page).to have_content("error")
            expect(TodoList.count).to eq(0)

            visit "/todo_lists"
            expect(page).to_not have_content("This is what I'm doing today.")
        end

        it "displays an error when the title has less than 3 characters" do
            expect(TodoList.count).to eq(0)

            create_todo_list title: "Hi"

            expect(page).to have_content("error")
            expect(TodoList.count).to equal(0)

            visit "/todo_lists"
            expect(page).to_not have_content("This is what I'm doing today.")
        end

        it "displays an error when the todo list has no description" do
            expect(TodoList.count).to eq(0) 

            create_todo_list title: "Grocery list", description: ""

            expect(page).to have_content("error")
            expect(TodoList.count).to eq(0)

            visit "/todo_lists"
            expect(page).to_not have_content("Grocery list")
        end

        it "displays an error when the todo list has no description" do
            expect(TodoList.count).to eq(0) 

            create_todo_list title: "Grocery list", description: "Food"

            expect(page).to have_content("error")
            expect(TodoList.count).to eq(0)

            visit "/todo_lists"
            expect(page).to_not have_content("Grocery list")
        end
    end

Here is my code for the edit_spec.rb file:

edit_spec.rb
require "spec_helper"

describe "Editing todo lists" do
    it "updates a todo list successfully with correct information" do
        todo_list = TodoList.create(title: "Grocery", description: "Grocery list.")

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

        fill_in "Title", with: "New title"
        fill_in "Description", with: "New description"
        click_button "Update Todo list"

        todo_list.reload

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

4 Answers

Hi Sarah,

Can you paste the results of running the tests so we can see what the failure is? The messages are usually pretty descriptive and help track down the issue - but they take a bit of getting used to!

Steve.

P.S. I edited your question so the code shows more clearly

Tom Finet
Tom Finet
7,027 Points

Thanks Steve. I'll post the results of the tests.

Tom Finet
Tom Finet
7,027 Points

Results to the failed tests:

create_spec.rb:

FFFFF

Failures:

1) Creating todo lists redirects to the todo list index page on success Failure/Error: expect(page).to have_content("New todo_list") expected to find text "New todo_list" in "New Todo List Title Description Back" # ./spec/features/todo_lists/create_spec.rb:10:in create_todo_list' # ./spec/features/todo_lists/create_spec.rb:18:inblock (2 levels) in <top (required)>'

2) Creating todo lists displays an error when the todo list has no title Failure/Error: expect(page).to have_content("New todo_list") expected to find text "New todo_list" in "New Todo List Title Description Back" # ./spec/features/todo_lists/create_spec.rb:10:in create_todo_list' # ./spec/features/todo_lists/create_spec.rb:25:inblock (2 levels) in <top (required)>'

3) Creating todo lists displays an error when the todo list has no description Failure/Error: expect(page).to have_content("New todo_list") expected to find text "New todo_list" in "New Todo List Title Description Back" # ./spec/features/todo_lists/create_spec.rb:10:in create_todo_list' # ./spec/features/todo_lists/create_spec.rb:49:inblock (2 levels) in <top (required)>'

4) Creating todo lists displays an error when the todo list has no description Failure/Error: expect(page).to have_content("New todo_list") expected to find text "New todo_list" in "New Todo List Title Description Back" # ./spec/features/todo_lists/create_spec.rb:10:in create_todo_list' # ./spec/features/todo_lists/create_spec.rb:61:inblock (2 levels) in <top (required)>'

5) Creating todo lists displays an error when the title has less than 3 characters Failure/Error: expect(page).to have_content("New todo_list") expected to find text "New todo_list" in "New Todo List Title Description Back" # ./spec/features/todo_lists/create_spec.rb:10:in create_todo_list' # ./spec/features/todo_lists/create_spec.rb:37:inblock (2 levels) in <top (required)>'

Finished in 0.28202 seconds 5 examples, 5 failures

Failed examples:

rspec ./spec/features/todo_lists/create_spec.rb:17 # Creating todo lists redirects to the todo list index page on success rspec ./spec/features/todo_lists/create_spec.rb:22 # Creating todo lists displays an error when the todo list has no title rspec ./spec/features/todo_lists/create_spec.rb:46 # Creating todo lists displays an error when the todo list has no description rspec ./spec/features/todo_lists/create_spec.rb:58 # Creating todo lists displays an error when the todo list has no description rspec ./spec/features/todo_lists/create_spec.rb:34 # Creating todo lists displays an error when the title has less than 3 characters

Randomized with seed 35843

edit_spec.rb:

F

Failures:

1) Editing todo lists updates a todo list successfully with correct information Failure/Error: expect(TodoList.title).to eq("New title") NoMethodError: undefined method title' for #<Class:0x007fc6f82815b0> # ./spec/features/todo_lists/edit_spec.rb:33:inblock (2 levels) in <top (required)>'

Failed examples:

rspec ./spec/features/todo_lists/edit_spec.rb:23 # Editing todo lists updates a todo list successfully with correct information

Randomized with seed 26047

Thanks a bunch!

Hi Sarah,

This is a common failure.

The key part is Failure/Error: expect(page).to have_content("New todo_list") expected to find text "New todo_list" in "New Todo List Title Description Back".

So, your test code is saying that your page should have the text "New Todo_list" on it. That, correctly, fails. Your page has, also correctly, the text "New Todo List" on it. You need to change your test to look for the right piece of text.

In your create_todo_list definition change:

expect(page).to have_content("New todo_list")

to

expect(page).to have_content("New Todo List")

Do you understand what that all means? I can go through that in more detail, if you want.

Essentially, your page is correct; your test is wrong. Your page should have the content the test is expecting so the test needs to change to fit the expected content. This happens a lot on the course - I wonder if the tutorial might be unclear at that point.

Steve.

Tom Finet
Tom Finet
7,027 Points

I have applied your advice to the file but there are still 3 errors in the create_spec.rb file out of 5 examples. In the edit_spec.rb file the error is still present. I don't really understand why.

I missed that one - sorry! Be right back ...

OK in edit_spec I think you've used the wrong reference to the todo list.

Try amending:

        expect(TodoList.title).to eq("New title")
        expect(TodoList.description).to eq("New description")

to

        expect(todo_list.title).to eq("New title")
        expect(todo_list.description).to eq("New description")

What errors is create_spec throwing now? Can you paste the messages?

Tom Finet
Tom Finet
7,027 Points

Since my first post my code has developed and changed. I will post it here:

create_spec.rb
    describe "Creating todo lists" do
        def create_todo_list(options={})
            options [:title] ||= "My todo list"
            options [:description] ||= "This is my todo list."

            visit "/todo_lists"
            click_link "New Todo list"
            expect(page).to have_content("New Todo List")

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

        it "redirects to the todo list index page on success" do
            create_todo_list
            expect(page).to have_content("My todo list")
        end

        it "displays an error when the todo list has no title" do
            expect(TodoList.count).to eq(0) 

            create_todo_list title: ""

            expect(page).to have_content("error")
            expect(TodoList.count).to eq(0)

            visit "/todo_lists"
            expect(page).to_not have_content("This is what I'm doing today.")
        end

        it "displays an error when the title has less than 3 characters" do
            expect(TodoList.count).to eq(0)

            create_todo_list title: "Hi"

            expect(page).to have_content("error")
            expect(TodoList.count).to equal(0)

            visit "/todo_lists"
            expect(page).to_not have_content("This is what I'm doing today.")
        end

        it "displays an error when the todo list has no description" do
            expect(TodoList.count).to eq(0) 

            create_todo_list title: "Grocery list", description: ""

            expect(page).to have_content("error")
            expect(TodoList.count).to eq(0)

            visit "/todo_lists"
            expect(page).to_not have_content("Grocery list")
        end

        it "displays an error when the todo list has no description" do
            expect(TodoList.count).to eq(0) 

            create_todo_list title: "Grocery list", description: "Food"

            expect(page).to have_content("error")
            expect(TodoList.count).to eq(0)

            visit "/todo_lists"
            expect(page).to_not have_content("Grocery list")
        end
    end
edit_spec.rb
describe "Editing todo lists" do

    let!(:todo_list) { TodoList.create(title: "Grocery", description: "Grocery list.") }

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

        todo_list = options[:todo_list]

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

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

    it "updates a todo list successfully with correct information" do
        todo_list = TodoList.create(title: "Grocery", description: "Grocery list.")

        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

    it "displays an error with no title" do
        update_todo_list todo_list: todo_list, title: ""
        title = todo_list.title
        todo_list.reload
        expect(todo_list.title).to eq(title)
        expect(page).to have_content("error")
    end

    it "displays an error with too short a title" do
        update_todo_list todo_list: todo_list, title: "hi"
        expect(page).to have_content("error")
    end

    it "displays an error with no description" do
        update_todo_list todo_list: todo_list, description: ""
        expect(page).to have_content("error")
    end

    it "displays an error with too short a description" do
        update_todo_list todo_list: todo_list, description: "hi"
        expect(page).to have_content("error")
    end
end

Thanks to your help I only have 3/5 errors on both files. All the remaining errors on both files are exactly the same. Here is the error:

Error for create_spec.rb:

F..FF

Failures:

1) Creating todo lists displays an error when the todo list has no description Failure/Error: expect(page).to have_content("error") expected to find text "error" in "Todo list was successfully created. Title: Grocery list Description: Edit | Back" # ./spec/features/todo_lists/create_spec.rb:51:in `block (2 levels) in <top (required)>'

2) Creating todo lists displays an error when the todo list has no description Failure/Error: expect(page).to have_content("error") expected to find text "error" in "Todo list was successfully created. Title: Grocery list Description: Food Edit | Back" # ./spec/features/todo_lists/create_spec.rb:63:in `block (2 levels) in <top (required)>'

3) Creating todo lists displays an error when the title has less than 3 characters Failure/Error: expect(page).to have_content("error") expected to find text "error" in "Todo list was successfully created. Title: Hi Description: This is my todo list. Edit | Back" # ./spec/features/todo_lists/create_spec.rb:39:in `block (2 levels) in <top (required)>'

Finished in 0.62742 seconds 5 examples, 3 failures

Failed examples:

rspec ./spec/features/todo_lists/create_spec.rb:46 # Creating todo lists displays an error when the todo list has no description rspec ./spec/features/todo_lists/create_spec.rb:58 # Creating todo lists displays an error when the todo list has no description rspec ./spec/features/todo_lists/create_spec.rb:34 # Creating todo lists displays an error when the title has less than 3 characters

Randomized with seed 1273

Error for edit_spec.rb:

FF.F.

Failures:

1) Editing todo lists displays an error with too short a description Failure/Error: expect(page).to have_content("error") expected to find text "error" in "Todo list was successfully updated. Title: My todo list Description: hi Edit | Back" # ./spec/features/todo_lists/edit_spec.rb:57:in `block (2 levels) in <top (required)>'

2) Editing todo lists displays an error with too short a title Failure/Error: expect(page).to have_content("error") expected to find text "error" in "Todo list was successfully updated. Title: hi Description: This is my todo list Edit | Back" # ./spec/features/todo_lists/edit_spec.rb:47:in `block (2 levels) in <top (required)>'

3) Editing todo lists displays an error with no description Failure/Error: expect(page).to have_content("error") expected to find text "error" in "Todo list was successfully updated. Title: My todo list Description: Edit | Back" # ./spec/features/todo_lists/edit_spec.rb:52:in `block (2 levels) in <top (required)>'

Finished in 0.54615 seconds 5 examples, 3 failures

Failed examples:

rspec ./spec/features/todo_lists/edit_spec.rb:55 # Editing todo lists displays an error with too short a description rspec ./spec/features/todo_lists/edit_spec.rb:45 # Editing todo lists displays an error with too short a title rspec ./spec/features/todo_lists/edit_spec.rb:50 # Editing todo lists displays an error with no description

Randomized with seed 20447

I think that all the problems are the same. I really appreciate the help.

PS: Sorry for the late reply.

Let's start with create_spec - there's a lot to do here but I think the same lesson can be carried over.

There are 3 failures. One test is repeated, or badly named. Description is tested twice for being blank but in one test it isn't blank.

The tests fail because the page is expecting to throw an error in the scenarios tested, i.e. no title, title less than 3 chars, blank description - plus one more; not sure what that is.

Have you added the validations into the models yet? In todo_list.rb you need some rules - have you applied those?

There will be 4, I think.

validates :title, presence: true
validates :title, length: { minimim: 3 }
validates :description, presence: true
validates :description, length: { minimum: 5 }

I guessed at the last one as you'd entered a four letter test description.

Do you have those validations?

Steve.

Once we've tidied up create_specwe can move on toedit_spec`! (although the error is the same, I think).

Tom Finet
Tom Finet
7,027 Points

Thank you so much! The validations have solved every error. I really appreciate the work you put into helping me!

No problem! :-)