Hello all:
I’m a RoR newbie who is currently following the Ruby on Rails
Tutorial: Learning by Example book (http://ruby.railstutorial.org/
chapters/modeling-users#sec:adding_a_secure_password). The following
Chapter 6 RSpec tests are failing:
Failures:
-
User [31mFailure/Error:[0m [31mit { should be_valid }[0m
[31mexpected valid? to return true, got false[0m [36m # ./spec/models/
user_spec.rb:19:in `block (2 levels) in '[0m -
User when email format is valid should be valid [31mFailure/Error:
[0m [[email protected] be_valid[0m [31mexpected valid? to return true,
got false[0m [36m # ./spec/models/user_spec.rb:51:in block (4 levels)
in <top (required)>‘[0m [36m # ./spec/models/user_spec.rb:
49:ineach’[0m [36m # ./spec/models/user_spec.rb:49:in `block (3
levels) in '[0m -
User return value of authenticate method with valid password
[31mFailure/Error:[0m [31mit { should ==
found_user.authenticate(@user.password) }[0m [31mNoMethodError:[0m
[31mundefined method authenticate’ for nil:NilClass←[0m [36m # ./spec/
models/user_spec.rb:94:inblock (4 levels) in '[0m -
User return value of authenticate method with invalid password
[31mFailure/Error:[0m [31mlet(:user_for_invalid_password)
{ found_user.authenticate(“invalid”) }[0m [31mNoMethodError:[0m
[31mundefined method authenticate’ for nil:NilClass[0m [36m # ./spec/
models/user_spec.rb:98:inblock (4 levels) in '[0m [36m # ./spec/models/
user_spec.rb:100:in `block (4 levels) in '[0m -
User return value of authenticate method with invalid password
[31mFailure/Error:[0m [31mlet(:user_for_invalid_password)
{ found_user.authenticate(“invalid”) }[0m [31mNoMethodError:[0m
[31mundefined method authenticate’ for nil:NilClass[0m [36m # ./spec/
models/user_spec.rb:98:inblock (4 levels) in '[0m [36m # ./spec/models/
user_spec.rb:101:in `block (4 levels) in '[0m
Finished in 3.56 seconds [31m20 examples, 5 failures[0m
Failed examples:
[31mrspec ./spec/models/user_spec.rb:19[0m [36m# User [0m [31mrspec ./
spec/models/user_spec.rb:47[0m [36m# User when email format is valid
should be valid←[0m [31mrspec ./spec/models/user_spec.rb:94[0m [36m#
User return value of authenticate method with valid password ←[0m
[31mrspec ./spec/models/user_spec.rb:100[0m [36m# User return value of
authenticate method with invalid password [0m [31mrspec ./spec/models/
user_spec.rb:101[0m [36m# User return value of authenticate method
with invalid password [0m Slave(1) run done!
Here is the user Model:
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
attr_accessor :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+-.]+@[a-z\d-.]+.[a-z]+\z/i
validates :email, presence: true, format: { with:
VALID_EMAIL_REGEX },uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
Here is the RSpec model/user_spec.rb:
require ‘spec_helper’
describe User do
before do
@user = User.new(name: “Examplexxx”, email: “[email protected]”,
password: “foobar”, password_confirmation:
“foobar”)
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
describe “when name is not present” do
before { @user.name = " " }
it { should_not be_valid }
end
describe “when email is not present” do
before { @user.email = " " }
it { should_not be_valid }
end
describe “when name is longer than 50 characters” do
before { @user.name = “a” * 51 }
it { should_not be_valid }
end
describe “when email format is invalid” do
it “should be invalid” do
addresses = %w[user@foo, com user_at_foo.org example.user@foo.
foo@bar_baz.com foo@bar+baz.com]
addresses.each do |invalid_address|
@user.email = invalid_address
@user.should_not be_valid
end
end
end
describe “when email format is valid” do
it “should be valid” do
addresses = %w[[email protected] [email protected] [email protected] a
[email protected]]
addresses.each do |valid_address|
@user.email = valid_address
@user.should be_valid
end
end
end
describe “when email address is already taken” do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe “when password is not present” do
before { @user.password = @user.password_confirmation = " " }
it { should_not be_valid }
end
describe “when password doesn’t match confirmation” do
before { @user.password_confirmation = “mismatch” }
it { should_not be_valid }
end
describe “When password confirmation is nil” do
before { @user.password_confirmation = nil }
it { should_not be_valid }
end
describe “with a password that’s too short” do
before { @user.password = @user.password_confirmation = “a” * 5 }
it { should_not be_valid }
end
describe “return value of authenticate method” do
before { @user.save }
let(:found_user) { User.find_by_email(@user.email) }
describe "with valid password" do
it { should == found_user.authenticate(@user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password)
{ found_user.authenticate(“invalid”) }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
end
Here is my gemfile:
source ‘https://rubygems.org’
gem ‘rails’, ‘3.2.3’
gem ‘bootstrap-sass’, ‘2.0.0’
gem ‘bcrypt-ruby’, ‘3.0.1’
group :development, :test do
gem ‘sqlite3’, ‘1.3.5’
gem ‘rspec-rails’, ‘2.9.0’
gem ‘spork’
gem ‘webrat’
end
gem ‘jquery-rails’, ‘2.0.0’
group :test do
gem ‘capybara’, ‘1.1.2’
end
Gems used only for assets and not required
in production environments by default.
group :assets do
gem ‘sass-rails’, ‘~> 3.2.3’
gem ‘coffee-rails’, ‘~> 3.2.2’
gem ‘uglifier’, ‘>= 1.0.3’
end
Thank you so much for your help! Neo Ramos