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 Adding Validations to Todo Items

Charles Harpke
Charles Harpke
33,986 Points

Adding Validation to todo items error in testing

run this test bin/rspec --format=documentation spec/features/todo_items/create_spec.rb, and here is my result: Failures:

1) Adding todo items displays an error with content less than 2 characters long Failure/Error: expect(page).to have_content("There was a problem adding that todo list item.") expected to find text "There was a problem adding that todo list item." in "There was a problem adding that todo list item" # ./spec/features/todo_items/create_spec.rb:41:in block (3 levels) in <top (required)>' # ./spec/features/todo_items/create_spec.rb:40:inblock (2 levels) in <top (required)>'

2) Adding todo items displays an error with no content Failure/Error: expect(page).to have_content("There was a problem adding that todo list item.") expected to find text "There was a problem adding that todo list item." in "There was a problem adding that todo list item" # ./spec/features/todo_items/create_spec.rb:30:in block (3 levels) in <top (required)>' # ./spec/features/todo_items/create_spec.rb:29:inblock (2 levels) in <top (required)>' here is my create spec file:

require 'spec_helper'

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

    def visit_todo_list(list)
       visit "/todo_lists"
       within "#todo_list_#{list.id}" do
       click_link "List Items"
   end
 end

it "is successful with valid content" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: "Milk"
    click_button "Save"
    expect(page).to have_content("Added todo list item.")
    within("ul.todo_items") do
     expect(page).to have_content("Milk")
    end
 end    

it "displays an error with no content" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: ""
    click_button "Save"
    within("div.flash") do
    expect(page).to have_content("There was a problem adding that todo list item.")
 end
    expect(page).to have_content("Content can't be blank")
 end

it "displays an error with content less than 2 characters long" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: "1"
    click_button "Save"
    within("div.flash") do
    expect(page).to have_content("There was a problem adding that todo list item.")
 end
    expect(page).to have_content("Content is too short")
 end

end

and my new.html.erb:

<%= form_for [@todo_list, @todo_item] do |form| %>
    <% if @todo_item.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@todo_item.errors.count, "error") %> prohibited this todo item from being saved:</h2>

      <ul>
      <% @todo_item.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


    <%= form.label :content %>
    <%= form.text_field :content %>

    <%= form.submit "Save" %>
<% end %>
Charles Harpke
Charles Harpke
33,986 Points
<%= form_for [@todo_list, @todo_item] do |form| %>
    <% if @todo_item.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@todo_item.errors.count, "error") %> prohibited this todo item from being saved:</h2>

      <ul>
      <% @todo_item.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


    <%= form.label :content %>
    <%= form.text_field :content %>

    <%= form.submit "Save" %>
<% end %>
Charles Harpke
Charles Harpke
33,986 Points

Here is my todo_item.rb class TodoItem < ActiveRecord::Base belongs_to :todo_list

validates :content, presence: true,
                    length: { minimum: 2 }

end

Charles Harpke
Charles Harpke
33,986 Points

I found it....argh I expected to find text "There was a problem adding that todo list item." in "There was a problem adding that todo list item"

I was expecting that the sentence ends with a period, but apparently it does not.