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
Lewis McLennan
18,400 PointsBuild a Todo List with Ruby error on rspec test for create_spec.rb
Hi All,
I'm currently on Writing Test Helpers stage of the Build a Todo List Application with Ruby, and I seem to be having a problem when I run rspec on my create_spec.rb
I am getting the following error.
ruby
Failures:
1) creating todo items displays an error with content less than 2 characters long
Failure/Error: expect(page).to have_content("Content is too short")
expected to find text "Content is too short" in "There was a problem adding that todo list item. Grocery List - Editing Todo List Item Content"
# ./spec/features/todo_items/create_spec.rb:37:in `block (2 levels) in <top (required)>'
2) creating todo items displays an error with no content
Failure/Error: expect(page).to have_content("Content can't be blank")
expected to find text "Content can't be blank" in "There was a problem adding that todo list item. Grocery List - Editing Todo List Item Content"
# ./spec/features/todo_items/create_spec.rb:26:in `block (2 levels) in <top (required)>'
Finished in 0.3253 seconds
3 examples, 2 failures
Failed examples:
rspec ./spec/features/todo_items/create_spec.rb:29 # creating todo items displays an error with content less than 2 characters long
rspec ./spec/features/todo_items/create_spec.rb:17 # creating todo items displays an error with no content
Any Ideas what might be causing it, a I believe its not displaying the error messages even though everything looks fine in my views. Please see below for my new and partial form views as well as my controller.
Controller
ruby
class TodoItemsController < ApplicationController
before_action :find_todo_list
def index
end
def new
@todo_item = @todo_list.todo_items.new
end
def create
@todo_list = TodoList.find(params[:todo_list_id])
@todo_item = @todo_list.todo_items.new(todo_item_params)
if @todo_item.save
flash[:success] = "Added todo list item."
redirect_to todo_list_todo_items_path
else
flash[:error] = "There was a problem adding that todo list item."
render action: :new
end
end
def update
@todo_item = @todo_list.todo_items.find(params[:id])
if @todo_item.update_attributes(todo_item_params)
flash[:success] = "Saved todo list item."
redirect_to todo_list_todo_items_path
else
flash[:error] = "That todo item could not be saved."
render action: :edit
end
end
def edit
@todo_item = @todo_list.todo_items.find(params[:id])
end
def destroy
@todo_item = @todo_list.todo_items.find(params[:id])
if @todo_item.destroy
flash[:success] = "Todo list item was deleted."
else
flash[:error] = "Todo list item could not be deleted."
end
redirect_to todo_list_todo_items_path
end
def url_options
{ todo_list_id: params[:todo_list_id]}.merge(super)
end
def find_todo_list
@todo_list = TodoList.find(params[:todo_list_id])
end
private
def todo_item_params
params[:todo_item].permit(:content)
end
end
My New view
ruby
<h1><%=@todo_list.title%> - Editing Todo List Item</h1>
<%=form_for [@todo_list, @todo_item] do |form|%>
<%=render partial: form%>
<%end%>
and my partial form
ruby
<% if @todo_list.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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<%end%>
<%=form.label :content%>
<%=form.text_field :content%>
<%=form.submit "Save"%>
I would be most grateful to anyone who can help me solve this.
Thanks
edit
My Create Spec
ruby
require 'spec_helper'
describe "creating todo items" do
let!(:todo_list) {TodoList.create(title: "Grocery List", desc: "Groceries")}
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
Lewis McLennan
18,400 Pointsmanually I get the message 'There was a problem adding that todo list item', but I don't get any message that the content is too short.
See screenshot https://www.evernote.com/shard/s298/sh/59f07a43-5652-4610-9113-0db6daa4c465/3ce11bba3ca448bca20b79cdb8d3ba19
Lewis McLennan
18,400 PointsAnyone got any ideas?
2 Answers
Lewis McLennan
18,400 PointsThis has now been fixed, was caused by the first line in the partial form as it was using the wrong instance variable
ruby
<% if @todo_list.errors.any? %>
should have been
ruby
<% if @todo_item.errors.any? %>
Chris Chambers
5,634 PointsPaste in your create_spec.rb please?
Lewis McLennan
18,400 PointsWhoops sorry, just added.
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 PointsCan you simulate this situation manually and see if that text is present on the page when the content is blank or too short?