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 Write Our First Tests

Blair Rorani
Blair Rorani
6,658 Points

Why do I get this failure "creating todo lists redirects to the todo list index page on success"?

create_spec.rb:

require 'spec_helper'
describe "creating todo lists" do
    it "redirects to the todo list index page on success" do
        visit "/todo_lists"
        click_link "New Todo list"
        expect(page).to have_content("New todo_list")
        fill_in "Title", with: "My todo list"
        fill_in "Description", with: "This is what I'm doing today"
        click_button "Create todo list"
        expect(page).to have_content("My todo list")
    end     
end

test result is:

1 example, 1 failure

Failed examples:

rspec ./spec/features/todo_lists/create_spec.rb:4 # creating todo lists redirects to the todo list index page on success

2 Answers

Your test makes sense. It would fail if you are not redirecting on click. I cannot tell where the error is without seeing the code from the route and the code from the html.erb file.

Those are the most likely places for your error to be.

Blair Rorani
Blair Rorani
6,658 Points

Even with out the redirect, the contents of the success page are:

Todo list was successfully created.

Title: My todo list

Description: This is a list.

Edit | Back


So this test should still work:

expect(page).to have_content("My todo list")
Thorstein Nordby
Thorstein Nordby
17,115 Points

Can't get this to work either....

Failed examples:

rspec ./spec/features/todo_lists/create_spec.rb:4 # Creating todo lists redirects to the todo list index page on success

require 'spec_helper'

describe "Creating todo lists" do 
    it "redirects to the todo list index page on success" do
        visit "/todo_lists"
        click_link "New Todo List"
        expect(page).to have_content("New todo_list")
    end
end

ยดยดยด
Baptiste Brassart
Baptiste Brassart
5,766 Points

Hi guys!

UPDATE: I followed a tip that seems to work

In your todo_list.rb, copy paste this:

require 'spec_helper'

describe "Creating todo lists" do
def create_todo_list(options={})
    options[:title] ||= "My todo list" #If we do not send in title, it's going to default be 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 #you may not have gotten to the point of refactoring this part
    expect(page).to have_content("My todo list")
  end
it "displays an error when 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 my todo list.")
  end
end

Maybe your problem comes also from that.

I'm using OS X 10.10.3 and I had the same error than you do. Here is my code:

require 'spec_helper'
describe "Creating todo lists" do
    it "redirects to the todo list index page on success" do
        visit "/todo_lists"
        click_link "New Todo list"
        expect(page).to have_content("New todo list")
        fill_in "Title", with: "My todo list"
        fill_in "Description", with: "This is what I'm doing today"
        click_button "Create Todo list"
        expect(page).to have_content("My todo list")
    end
end

The error I receive on the console after passing

bin/rspec spec/features/todo_lists/create_spec.rb

is the following:

2 deprecation warnings total

Finished in 0.20748 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/todo_lists/create_spec.rb:4 # Creating todo lists redirects to the todo list index page on success

Cheers