Ruby on Rails Tutorial - Getting Errors, pulling my hair out - ruby-on-rails

Im following [Michael Hartl's tutorial][1] and did the exercises in Chapter 7, and now have 4 errors that I cant figure out how to fix for the life of me. When I test the production app manually, the errors dont exist at all. So I don't know if there is something wrong with my text development or something, but Im at a total loss so I thought I'd post here to see if my total noobness is blinding me...thanks for your help!
Here's the 4 error messages I'm getting:
Failures:
1) signup with invalid information after submission
Failure/Error: it { should have_selector('title', text: "Sign up") }
expected css "title" with text "Sign up" to return something
# ./spec/requests/user_pages_spec.rb:38:in `block (4 levels) in <top (required)>'
2) signup with invalid information after submission
Failure/Error: it { should have_content('error') }
expected there to be content "error" in "after submission"
# ./spec/requests/user_pages_spec.rb:39:in `block (4 levels) in <top (required)>'
3) signup after saving the user
Failure/Error: it { should have_selector('title', text: user.name) }
NoMethodError:
undefined method `name' for nil:NilClass
# ./spec/requests/user_pages_spec.rb:60:in `block (3 levels) in <top (required)>'
4) signup after saving the user
Failure/Error: it { should have_selector('div.alert.alert-success', text: 'Welcome') }
expected css "div.alert.alert-success" with text "Welcome" to return something
# ./spec/requests/user_pages_spec.rb:61:in `block (3 levels) in <top (required)>'
Finished in 6.8 seconds
10 examples, 4 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:38 # signup with invalid information after submission
rspec ./spec/requests/user_pages_spec.rb:39 # signup with invalid information after submission
rspec ./spec/requests/user_pages_spec.rb:60 # signup after saving the user
rspec ./spec/requests/user_pages_spec.rb:61 # signup after saving the user
Here's the code on my user_pages_spec.rb:
require 'spec_helper'
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
describe "after submission" do
before { click_button submit }
it { should have_selector('title', text: "Sign up") }
it { should have_content('error') }
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by_email('user#example.com') }
it { should have_selector('title', text: user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
end
[1]: http://ruby.railstutorial.org/
Here's the template code for views/users/show.html.erb
<% provide(:title, #user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
</aside>
</div>
and then here's the users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render 'new'
end
end
end

Allright y'all,
I dont know if not answering my question was some sort of torturous initiation for noobs in this forum, but after almost 24 hours and a good night sleep, I solved the problem!
After a few G searches, I found that I could be stopping some of the variables from passing through by having "end" in the wrong places. It turns out there were 2 main areas where I was doing this. Once I found and fixed those, all the errors went away.
I will now pat myself on the back. I hope this helps any total noobs who run into this same problem in the future.

Related

Failure/Error with Ruby on Rails Tutorial Chapter 9

Can anyone who has done this exercises help me out?
It's my first time using this programming language and not much to this, but I need to finish well until chapter 10 that is a final evaluation.
1) User pages edit with valid information
Failure/Error: describe "page" do
NoMethodError:
undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc283f8c>
# ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'
2) User pages edit with valid information
Failure/Error: describe "page" do
NoMethodError:
undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xb67b1cc>
# ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'
3) User pages edit with valid information
Failure/Error: describe "page" do
NoMethodError:
undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc224244>
# ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'
4) User pages edit with valid information
Failure/Error: describe "page" do
NoMethodError:
undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc562690>
# ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'
5) User pages edit with valid information
Failure/Error: describe "page" do
NoMethodError:
undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc0559a4>
# ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'
6) User pages index
Failure/Error: visit users_path
ActionView::Template::Error:
undefined method `each' for nil:NilClass
# ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb__615083638_97671840'
# ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'
7) User pages index
Failure/Error: visit users_path
ActionView::Template::Error:
undefined method `each' for nil:NilClass
# ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb__615083638_97671840'
# ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'
8) User pages index should list each user
Failure/Error: visit users_path
ActionView::Template::Error:
undefined method `each' for nil:NilClass
# ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb__615083638_97671840'
# ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'
9) Authentication authorization after signing in should render the desired protected page
Failure/Error: expect(page).to have_title('Edit user')
expected #has_title?("Edit user") to return true, got false
# ./spec/requests/authentication_pages_spec.rb:84:in `block (4 levels) in <top (required)>'
Finished in 2.25 seconds
72 examples, 9 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:114 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:115 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:112 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:113 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:111 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:15 # User pages index
rspec ./spec/requests/user_pages_spec.rb:16 # User pages index
rspec ./spec/requests/user_pages_spec.rb:18 # User pages index should list each user
rspec ./spec/requests/authentication_pages_spec.rb:83 # Authentication authorization after signing in should render the desired protected page
Edit
Here is my code:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "index" do
before do
sign_in FactoryGirl.create(:user)
FactoryGirl.create(:user, name: "Bob", email: "bob#example.com")
FactoryGirl.create(:user, name: "Ben", email: "ben#example.com")
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
it "should list each user" do
User.all.each do |user|
expect(page).to have_selector('li', text: user.name)
end
end
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
describe "after submission" do
before { click_button submit }
it { should have_title('Sign up') }
it { should have_content('error') }
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by(email: 'user#example.com') }
it { should have_link('Sign out') }
it { should have_title(user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
end
end
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit edit_user_path(user)
describe "page" do
it { should have_content("Update your profile") }
it { should have_title("Edit user") }
it { should have_link('change', href: 'http://gravatar.com/emails') }
end
describe "with invalid information" do
before { click_button "Save changes" }
it { should have_content('error') }
end
end
describe "with valid information" do
let(:new_name) { "New Name" }
let(:new_email) { "new#example.com" }
before do
fill_in "Name", with: new_name
fill_in "Email", with: new_email
fill_in "Password", with: user.password
fill_in "Confirm Password", with: user.password
click_button "Save changes"
end
it { should have_title(new_name) }
it { should have_selector('div.alert.alert-success') }
it { should have_link('Sign out', href: signout_path) }
specify { expect(user.reload.name).to eq new_name }
specify { expect(user.reload.email).to eq new_email }
end
end
end
If your code is formatted like this:
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit edit_user_path(user)
describe "page" do
it { should have_content("Update your profile") }
it { should have_title("Edit user") }
it { should have_link('change', href: 'http://gravatar.com/emails') }
end
I can see that it is probably broken without reading any of your code.
Are all your code blocks properly closed? Does not look so!
In addition to the unclosed blocks that phoet pointed out, which are causing your "undefined method describe" errors, you're getting errors in /app/views/users/index.html.erb line 5. The error is undefined method `each' for nil:NilClass which means there's a variable on that line that you're doing variable.each with, but you're either not setting that variable or when you're setting it, it's getting set to nil.
In general, the way for you to successfully get through this is to read the error messages and try to fix what they're complaining about. If you search for each error message individually, you'll find stack overflow questions about the specific issue that will help you.

Michael Hartl's Ruby on Rails tutorial failed test Chapter 8

I'm new on Rails. I'm following the tutorial of Michael Hartl, Ruby on Rails Tutorial - Learn Web Development with Rails (http://ruby.railstutorial.org/). I've found an error when I try to pass some tests. My output when I execute bundle exec rspec is the next:
.........................................F.....
Failures:
1) Authentication signin with invalid information
Failure/Error: it { should have_selector('div.alert.alert-error', text: 'Invalid') }
expected css "div.alert.alert-error" with text "Invalid" to return something
# ./spec/requests/authentication_pages_spec.rb:23:in `block (4 levels) in <top (required)>'
Finished in 1.39 seconds
47 examples, 1 failure
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:23 # Authentication signin with invalid information
The next files have been more highly changed and I think one of them may be causing the error:
authentication_pages_rspec.rb
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: 'Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_selector('title', text: 'Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end
end
end
sessions_controler.rb
class SessionsController `>` ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
sessions_helper.rb
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user;
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
#current_user = user
end
def current_user
#current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
end
If you need any other file, please let me know and I'll post it. Thank you in advance.
I've browsed through your code, and apart from the semicolon in
self.current_user = user;
I haven't seen anything strange. You haven't posted the method you're testing,
if user && user.authenticate(params[:session][:password])
though, so the problem might be there.
General steps to find the issue:
restart spark if you're using it.
test the failing spec manually: does the functionality work?
if not, fix it
if the functionality should work, but the spec still fails, use launchy and save_and_open_page to peek at the page during your test.
Addition 1:
well, that part in your spec
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
leads you to the SessionController create action (because the link links to signin_path which is mapped there in config/routes.rb) and the main logic that could go wrong is user.authenticate(params[:session][:password]) - which is provided by has_secure_password.

Ruby on Rails Tutorial by Michael Hartl. Failing test in Chapter 8.29

I am a newbie working through Hartl. Got to the end of Chapter 8 and when I check my browser for the sign in/sign out every things seems AOK. However when I run this test:
$ bundle exec spec spec/
returns
sis-macbook-pro:sample_app Lagaspi$ bundle exec rspec spec/
..............................F
Failures:
1) User pages signup with valid information after saving the user
Failure/Error: it { should have_link('Sign out') }
expected link "Sign out" to return something
# ./spec/requests/user_pages_spec.rb:48:in `block (5 levels) in <top (required)>'
Finished in 0.60715 seconds
31 examples, 1 failure
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:48 # User pages signup with valid information after saving the user
I have an idea but i'm not exactly sure. So here's my user_pages_spec.rb file:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button submit }
it { should have_link('Sign out') }
end
end
end
end
Go to http://ruby.railstutorial.org/chapters/sign-up?version=3.2#code-after_save_tests, and add those lines into you spec.
could you post your view file? Without it, it's difficult to tell what's wrong. From what I'm seeing, it only says you don't have the link.

Ruby on Rails Tutorial by Michael Hartl Chapter 7.25

Hi i'm a newbie working through Hartl. After a recent disaster I've been able to restore my app to a near working state (thanks Github!). I am now down to one error in my tests. So when I run the test I get:
sis-macbook-pro:sample_app Lagaspi$ bundle exec rspec spec/
..............................F
Failures:
1) User pages signup with valid information after saving the user
Failure/Error: it { should have_link('Sign out') }
expected link "Sign out" to return something
# ./spec/requests/user_pages_spec.rb:48:in `block (5 levels) in <top (required)>'
Finished in 0.61988 seconds
31 examples, 1 failure
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:48 # User pages signup with valid information after saving the user
Here's my user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
describe "signup" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button submit }
it { should have_link('Sign out') }
end
end
end
end
Thanks in advance.
Please check your 'Sign out' link , probably something goes wrong there.

Chapter 7 Ruby on Rails users_controller.rb error

I followed all the instructions in chapter 7 Ruby on Rails tutorial, when I finally run $ Bundle exec rspec spec/ a series of errors are being displayed. I doubled checked if my codes had any spelling mistakes or any symbols left out. Everything seems to fine. Could you please kindly interpret the errors and tell me where I am going wrong??? Thanks
USER_PAGES_SPEC.RB
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before {visit signup_path}
it { should have_selector('h1', text: 'Sign ') }
it { should have_selector('title', text: full_title('Sign up')) }
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving a user" do
before {click_button submit}
let (:user) {user.find_by_email("user#example.com")}
it {should have_selector('title', text:user.name)}
end
end
end
end
USER_CONTROLLER.RB
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render 'new'
end
end
end
ERRORS DISPLAYED ON COMMAND LINE
3) User pages signup page
←[31mFailure/Error:←[0m ←[31mit { should have_selector('title', text: full_
title('Sign up')) }←[0m
←[31mNoMethodError:←[0m
←[31mundefined method full_title' for #<RSpec::Core::ExampleGroup::Neste
d_2::Nested_1:0x461b260>←[0m
←[36m # ./spec/requests/user_pages_spec.rb:11:inblock (3 levels) in '←[0m
4) User pages profile page
←[31mFailure/Error:←[0m ←[31mit { should have_selector('title', text: user.
name) }←[0m
←[31mexpected css "title" with text "Michael Hartl" to return something←[
0m
←[36m # ./spec/requests/user_pages_spec.rb:19:in `block (3 levels) in '←[0m
5) User pages signup with valid information should create a user
←[31mFailure/Error:←[0m ←[31mexpect { click_button submit }.to change(User,
:count).by(1)←[0m
←[31mNoMethodError:←[0m
←[31mundefined method sign_in' for #<UsersController:0x45a8f00>←[0m
←[36m # ./app/controllers/users_controller.rb:14:increate'←[0m
←[36m # (eval):2:in click_button'←[0m
←[36m # ./spec/requests/user_pages_spec.rb:43:inblock (5 levels) in '←[0m
←[36m # ./spec/requests/user_pages_spec.rb:43:in `block (4 levels) in '←[0m
6) User pages signup with valid information after saving a user
←[31mFailure/Error:←[0m ←[31mbefore {click_button submit}←[0m
←[31mNoMethodError:←[0m
←[31mundefined method sign_in' for #<UsersController:0x46341c0>←[0m
←[36m # ./app/controllers/users_controller.rb:14:increate'←[0m
←[36m # (eval):2:in click_button'←[0m
←[36m # ./spec/requests/user_pages_spec.rb:47:inblock (5 levels) in '←[0m
1) undefined method full_title
Are you defined it?
2) expected css "title" with text "Michael Hartl" to return something
You haven't <title>Michael Hartl</title> in output html.
3, 4) undefined method sign_in
Where are you defined it?
Correct your code and all tests will be passed.

Resources