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 trialsungwon choe
15,044 Points"Write Our First Tests" no title failures even after validation in Rails 4.1.4
In the video, the rspec test for it "displays an error when the todo list has no title" passes after adding validations to the model:
class TodoList < ActiveRecord::Base
validates :title, presence: true
end
But mine still fails. I guess this is somehow related to using a different Rails version, but how?
3 Answers
sungwon choe
15,044 PointsAh man, I found my own mistake. I had:
expect(TodoList).to eq(0)
instead of
expect(TodoList.count).to eq(0)
J Scott Erickson
11,883 PointsMost likely that the flash message isn't being set properly. Can we see the relevant code? (controller / spec file)
J Scott Erickson
11,883 PointsI may have read wrong the first time. Its either the flash message or possibly the spec test itself? Probably need to see the spec.
sungwon choe
15,044 PointsHi J Scott, Thanks very much for the reply. My create_spec.rb is as follows.
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
it "displays an error when the todo list has no title" do
expect(TodoList).to eq(0)
visit "/todo_lists"
click_link "New Todo list"
expect(page).to have_content("New todo_list")
fill_in "Title", with: ""
fill_in "Description", with: "This is what I'm doing today"
click_button "Create Todo list"
expect(page).to have_content("error")
expect(TodoList).to eq(0)
visit "/todo_lists"
expect(page).to_not have_content("This is what I'm doing today")
end
end
Anthony Ho
10,228 PointsHi Sungwon Choe, I'm having no title after validation on rails. I've even used your create_spec.rb file, but it still fails. I can't figure out what's wrong.
sungwon choe
15,044 PointsHi Anthony,
My create_spec.rb above gets the error unless you change:
expect(TodoList).to eq(0)
to
expect(TodoList.count).to eq(0)
because I was comparing an object (TodoList) to a number (0) instead of a number (TodoList.count) to a number (0).
Did you try that?
Anthony Ho
10,228 PointsYeh I've done it. Turns I had expect(Todolist.count).to eq(0)
instead of
expect(TodoList.count).to eq(0)
It took a while to find that annoying bug.