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 trialAlan Springle
2,146 PointsRails 4 Todo List - Test Failure
Good morning,
I'm currently working on Build a Todo List Application with Rails 4, lesson Write our First Test. After I run the first test, I get an error and the test fails. I've gone through the lessons and double checked to make sure that I do not have any typos or missing items. I've read trough the forums, but have not found anything that has resolved my issue.
I'm using the Treehouse VM for Windows and setup detailed in the Installing a Ruby Development Environment lessons. Perhaps the Treehouse VM that I'm using is slightly different than the version in the videos?
I've taken screenshots of the gem file, spec_helper, create_spec, and the error that I'm getting. At this point, I can not move forward and I'm at a stopping point. Any help would be greatly appreciated!
Thanks,
Alan
Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.0.1'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :development, :test do
gem 'rspec-rails', '~> 2.0'
end
group :test do
gem 'capybara', '~> 2.1.0'
end
spec_helper:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
create_spec:
require 'spec_helper'
describe "Creating todo lists" do
it "redirects to the todo list index page on success" do
vist "/todo_lists"
click_link "New Todo list"
expect(page).to have_content("New todo_list")
end
end
error:
4 Answers
Maciej Czuchnowski
36,441 PointsOK, now I noticed a typo in your test: it says vist
instead of visit
. Fix that and see if it works now.
Maciej Czuchnowski
36,441 PointsCan you show us your controller and view for this? Also, it is better to copy the code and paste it here using ruby markdown (see "Markdown Cheatsheet" below, just above the Post answer button).
Alan Springle
2,146 PointsSure, here is the code below.
Thanks, Alan
todo_lisis_controller.rb:
class TodoListsController < ApplicationController
before_action :set_todo_list, only: [:show, :edit, :update, :destroy]
# GET /todo_lists
# GET /todo_lists.json
def index
@todo_lists = TodoList.all
end
# GET /todo_lists/1
# GET /todo_lists/1.json
def show
end
# GET /todo_lists/new
def new
@todo_list = TodoList.new
end
# GET /todo_lists/1/edit
def edit
end
# POST /todo_lists
# POST /todo_lists.json
def create
@todo_list = TodoList.new(todo_list_params)
respond_to do |format|
if @todo_list.save
format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
format.json { render action: 'show', status: :created, location: @todo_list }
else
format.html { render action: 'new' }
format.json { render json: @todo_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /todo_lists/1
# PATCH/PUT /todo_lists/1.json
def update
respond_to do |format|
if @todo_list.update(todo_list_params)
format.html { redirect_to @todo_list, notice: 'Todo list was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @todo_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todo_lists/1
# DELETE /todo_lists/1.json
def destroy
@todo_list.destroy
respond_to do |format|
format.html { redirect_to todo_lists_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_todo_list
@todo_list = TodoList.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def todo_list_params
params.require(:todo_list).permit(:title, :description)
end
end
new.html.erb:
<h1>New todo_list</h1>
<%= render 'form' %>
<%= link_to 'Back', todo_lists_path %>
index.html.erb:
<h1>Listing todo_lists</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @todo_lists.each do |todo_list| %>
<tr>
<td><%= todo_list.title %></td>
<td><%= todo_list.description %></td>
<td><%= link_to 'Show', todo_list %></td>
<td><%= link_to 'Edit', edit_todo_list_path(todo_list) %></td>
<td><%= link_to 'Destroy', todo_list, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Todo list', new_todo_list_path %>
Alan Springle
2,146 PointsIssue resolved, it was a typo on my part. Thanks for the assistance. Here is the correct 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")
end
end
Alan Springle
2,146 PointsAlan Springle
2,146 PointsUnfortunately after changing that, I still get the same error. The video has visit "/todo_lists" and the instructor's test passes. Also, I just noticed something... When I create a new item, the application does not redirect to the index page on success as the test suggests. When a new item is successfully created, you are sent to http://localhost:3000/todo_lists/5 (or 6,7,8, etc) which shows the item you created. Perhaps this is the problem?
Thanks,
Alan