ruby - rails tutorial 2nd edition. chapter 6.3.4 -
i hope can help. going through michael hartl's rails tutorial book , stuck on chapter 6.3.4. trying tests validate , keep getting several errors. have posted error messages , user.rb , user_spec.rb file reference. can see going wrong. appreciated.
errors
failures: 1) when email address taken ←[31mfailure/error:←[0m ←[31muser_with_same_email = @user.dup←[0m ←[31mtypeerror:←[0m ←[31mcan't dup nilclass←[0m ←[36m # ./spec/models/user_spec.rb:69:in `dup'←[0m ←[36m # ./spec/models/user_spec.rb:69:in `block (5 levels) in <top (required )>'←[0m 2) when password not present ←[31mfailure/error:←[0m ←[31mbefore { @user.password = @user.password_confi rmation = " " }←[0m ←[31mnomethoderror:←[0m ←[31mundefined method `password_confirmation=' nil:nilclass←[0m ←[36m # ./spec/models/user_spec.rb:78:in `block (5 levels) in <top (required )>'←[0m 3) when password doesn't match confirmation ←[31mfailure/error:←[0m ←[31mbefore { @user.password_confirmation = "mismat ch" }←[0m ←[31mnomethoderror:←[0m ←[31mundefined method `password_confirmation=' nil:nilclass←[0m ←[36m # ./spec/models/user_spec.rb:83:in `block (5 levels) in <top (required )>'←[0m 4) when password confirmation nil ←[31mfailure/error:←[0m ←[31mbefore { @user.password_confirmation = nil }←[ 0m ←[31mnomethoderror:←[0m ←[31mundefined method `password_confirmation=' nil:nilclass←[0m ←[36m # ./spec/models/user_spec.rb:88:in `block (5 levels) in <top (required )>'←[0m 5) password that's short ←[31mfailure/error:←[0m ←[31mbefore { @user.password = @user.password_confi rmation = "a" * 5 }←[0m ←[31mnomethoderror:←[0m ←[31mundefined method `password_confirmation=' nil:nilclass←[0m ←[36m # ./spec/models/user_spec.rb:93:in `block (5 levels) in <top (required )>'←[0m 6) return value of authticate method valid password ←[31mfailure/error:←[0m ←[31mbefore { @user.save }←[0m ←[31mnomethoderror:←[0m ←[31mundefined method `save' nil:nilclass←[0m ←[36m # ./spec/models/user_spec.rb:98:in `block (5 levels) in <top (required )>'←[0m 7) return value of authticate method invalid password ←[31mfailure/error:←[0m ←[31mbefore { @user.save }←[0m ←[31mnomethoderror:←[0m ←[31mundefined method `save' nil:nilclass←[0m ←[36m # ./spec/models/user_spec.rb:98:in `block (5 levels) in <top (required )>'←[0m
user.rb file
class user < activerecord::base attr_accessible :name, :email, :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 end
user_spec.rb
require 'spec_helper'
describe user before @user = user.new( name: "example user", email: "user@example.com", password: "foobar", password_confirmation: "foobar") end subject { @user } { should respond_to(:name) } { should respond_to(:email) } { should respond_to(:password_digest) } { should respond_to(:password) } { should respond_to(:password_confirmation) } { should be_valid } { should respond_to(:authenticate) } describe "when name not present" before { @user.name = " " } { should_not be_valid } end describe "when email not present" before { @user.email = " " } { should_not be_valid } end describe "when name long" before { @user.name = "a" * 51 } { should_not be_valid } end describe "when email format invalid" "should invalid" addresses = %w[user@foo,com user_at_foo.org example.user@foo. foo@bar_baz.com foo@bar+baz.com] addresses.each |invalid_address| @user.email = invalid_address @user.should_not be_valid end end end describe "when email format valid" "should valid" addresses = %w[user@foo.com a_us-er@f.b.org frst.lst@foo.jp a+b@baz.cn] addresses.each |valid_address| @user.email = valid_address @user.should be_valid end describe "when email address taken" before user_with_same_email = @user.dup user_with_same_email.email = @user.email.upcase user_with_same_email.save end { should_not be_valid } end describe "when password not present" before { @user.password = @user.password_confirmation = " " } { should_not be_valid } end describe "when password doesn't match confirmation" before { @user.password_confirmation = "mismatch" } { should_not be_valid } end describe "when password confirmation nil" before { @user.password_confirmation = nil } { should_not be_valid } end describe "with password that's short" before { @user.password = @user.password_confirmation = "a" * 5 } { should be_invalid } end describe "return value of authticate method" before { @user.save } let(:found_user) { user.find_by_email(@user.email) } describe "with valid password" { should == found_user.authenticate(@user.password) } end describe "with invalid password" let(:user_for_invalid_password) { found_user.authenticate("invalid") } specify { user_for_invalid_password.should be_false } end end end end
end
in user.rb, validates have incorrect syntax.
edit - also, check code directly against yours. has git repository here.
- it looks have different differences in user_spec compared his.
instead of this:
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
do this:
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
Comments
Post a Comment