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

Jason Chow
Jason Chow
4,428 Points

can't pass edit_spec.rb

MY CODE:

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 description" do
    update_todo_list todo_list: todo_list, description: "hi" 
    expect(page).to have_content("error")
end
end

ERROR MESSAGE:

Failures:

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

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: New title Description: New description Edit | Back" # ./spec/features/todo_lists/edit_spec.rb:44:in `block (2 levels) in '

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

4) 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: New title Description: New description Edit | Back" # ./spec/features/todo_lists/edit_spec.rb:49:in `b

help please.

6 Answers

Yes, this is where your problems lie. So, you've written two tests for each element, title and description. Those tests should fail if the element is blank, or too short. Your tests contain short and blank variants of title and description to pass into the update_todo_list method. Unfortunately, this method completely ignores the options{} part of the incoming parameter. Your method has hard-coded values for both title and description which are neither too short nor blank so your tests will fail:

        fill_in "Title", with: "New title"
        fill_in "Description", with: "New description"

I would recommend rewatching the video to see what amendments were made to this method; you might have missed a couple of tweaks out. But I think it'll end up replacing the above two lines with something like:

fill_in "Title", with: options[:title]
fill_in "Description", with: options[:description]

Give that a try to see if it works, else go back to the video to see what the amended code should look like.

Steve.

Hi Jason,

These tests are failing because the tests are expecting find the word "error" in the page following submission of a todo_list with too short (or no) description and/or title. Your code is allowing the changes to be made so there is something wrong with your validation.

Can you post your todo_list.rb file from models, please?

You should have something like:

validates :title, presence: true
validates :title, length: { mimimum: 3 }

Steve.

Jason Chow
Jason Chow
4,428 Points

My model.

class TodoList < ActiveRecord::Base
  validates :title, presence: true  
  validates :title, length: { minimum: 3}   
  validates :description, presence: true
  validates :description, length: { minimum: 5 }
end

This is confusing! Is your project in GitHub at all? If I can clone it here, that might make it easier to catch the bug.

Let's take one test at a time - the issue is common across them all, though - and try the "no title" test first.

You're setting the title to be blank with this code in the test:

update_todo_list todo_list: todo_list, title: "" 

That sets the title to be a blank string and passes that in the hash to update_todo_list method. (Can you paste me the code for that, please. Let's make sure that's correct).

So once you've sent the method the blank title the code ignores that and displays "New title" instead of a blank one:

Editing todo lists displays an error with no title Failure/Error: expect(page).to have_content("error") expected to find text "error" in "Todo list was successfully updated. Title: New title

So, I'm hoping there's something wrong with your update_todo_list method, as I can't see where else the problem could lie! So, two things; post me that method and/or let me have a look at your GitHub repo.

Steve.

Jason Chow
Jason Chow
4,428 Points

this is my method!

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: "New title"
        fill_in "Description", with: "New description"
        click_button "Update Todo list"
        todo_list.reload
    end

if there's nothing wrong here, how do i show you my Github repo? thanks!

Jason Chow
Jason Chow
4,428 Points

It passed! I don't know what i might have missed but i understood your explanation. Thank you so much Steve, it was very helpful :)