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
Naomi Freeman
Treehouse Guest TeacherRSpec weirdness - Rails ToDo App
I wrote the second test for the Rails ToDo App in Rspec, as the tutorial said.
The test initially told me I didn't have enough "end"s, so I randomly added one at the bottom and it ran and failed as expected.
I wrote the code to validate it, in the model: class TodoList < ActiveRecord::Base validates :title, presence: true end
The code is functioning on the app page when the server is fired up. It won't let you add a todo list item without a title.
But my test is failing.
?
Here's the Terminal error:
vagrant@precise64:~/hacker_you/projects/odot$ rspec spec/features/todo_lists/create_spec.rb [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message. .F
Failures:
1) Creating todolists displays an error when the todo list has no title
Failure/Error: it "redirects to the todo list index page on success" do
NoMethodError:
undefined method it' for #<RSpec::Core::ExampleGroup::Nested_1:0x007ff5f735eef0>
# ./spec/features/todo_lists/create_spec.rb:19:inblock (2 levels) in <top (required)>'
Finished in 0.43007 seconds 2 examples, 1 failure
Failed examples:
rspec ./spec/features/todo_lists/create_spec.rb:16 # Creating todolists displays an error when the todo list has no title
Randomized with seed 13702
--
Here's the test file:
require 'spec_helper'
describe "Creating todolists" 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.count).to eq(0)
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: ""
fill_in "Description", with: "This is what I'm doing today."
click_button "Create Todo list"
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 end
2 Answers
Naomi Freeman
Treehouse Guest TeacherWhoa. No idea how half my code looks awesome and some of it fell outside the realm of the cool black box. Sorry :/
Stephen Whitfield
16,771 PointsLooks like you forgot to include the block termination code "end" to your "it displays an error when the todo list has no title" block. Add it so it looks like this:
it "displays an error when the todo list has no title" do
expect(TodoList.count).to eq(0)
end
Naomi Freeman
Treehouse Guest TeacherNaomi Freeman
Treehouse Guest TeacherUgh I "answered" instead of commenting and now I worry no one will look at my question :(
I added another test, and now it's only seeing 2 tests. The title test is still failing, while the new test which should fail is not. It says 2 examples, 1 failure instead of 3 examples and 1 or 0 failures.
Validations and testing actually kill the real app I'm working on too. What am I doing wrong guys?