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 Creating Methods in Tests

Kosuke Kondo
Kosuke Kondo
1,235 Points

Could you tell me correct code?

This is my code. This error message was happened.

<My code> 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 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 end

<Error message> 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:23:inblock (2 levels) in <top (required)>'

Finished in 0.40802 seconds (files took 4.76 seconds to load) 2 examples, 2 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

Andrew Akers
Andrew Akers
404 Points

It seems to be finding the content: "New Todo List Title Description Back"

Instead of: "New todo_list" like you have in your expectation.

Perhaps, your expectation should look like: expect(page).to have_content("New Todo List")

Kosuke Kondo
Kosuke Kondo
1,235 Points

Thank you for your help, Andrew! :)

2 Answers

Daniel Crews
Daniel Crews
14,008 Points
require 'spec_helper'

describe "Creating todo lists" do
  def create_todo_list(options={})
    options[:title] ||= "My Todo Lists"
    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 Lists")
  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 todo list has a title 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 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 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
Kosuke Kondo
Kosuke Kondo
1,235 Points

I solved it by myself. I'm sorry if you started to help me. I'm glad that you would help me if I have some problems.

Kosuke Kondo
Kosuke Kondo
1,235 Points

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 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