infinity
infinity

Reputation: 719

Rails Tutorial Authenticate Issue

I'm following the Rails Tutorial Modeling Users Chapter: http://www.railstutorial.org/book/modeling_users#cha-modeling_users.

My user.rb looks like:

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  has_secure_password
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence:   true,
                format:     { with: VALID_EMAIL_REGEX },
                uniqueness: { case_sensitive: false }

  validates :password, length: { minimum: 6 }
end

and my user model spec looks like:

describe User, :type => :model do

   before do
     @user = User.new(name: "Example User", 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 }

   ... (other methods are here) 

describe "when password is not present" do
   before do
     @user = User.new(name: "Example User", email: "[email protected]",
                   password: "foobar", password_confirmation: "foobar")
   end
   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 eq found_user.authenticate(@user.password) }
   end

   describe "with invalid password" do
     let(:user_for_invalid_password) { found_user.authenticate("invalid") }

     it { should_not eq user_for_invalid_password }
     specify { expect(user_for_invalid_password).to be_false }
   end
end

Which I'm pretty sure is exactly a duplication what the Rails Tutorial code is, but I'm getting the following failed test errors:

rspec ./spec/models/user_spec.rb:83 # User when password is not present should not be valid
rspec ./spec/models/user_spec.rb:108 # User return value of authenticate method with invalid password should be false

Upvotes: 1

Views: 442

Answers (2)

jyrkim
jyrkim

Reputation: 2869

I checked this out by looking at the Ruby on Rails Tutorial Book's source code (Rails 4) at GitHub: spec/models/user_spec.rb. Based on the code there, it looks like your passwords are currently of acceptable type and that's why your test is failing. I mean your passwords are good. foobar is a valid password. Below an empty string is passed to the User model validation.

  describe "when password is not present" do
    before do
      @user = User.new(name: "Example User", email: "[email protected]",
                       password: " ", password_confirmation: " ")
    end
    it { should_not be_valid }
  end

The second one I'm not sure, but would it help if you tried the Rails 3 spec/models/user_spec.rbrelated code for the same test:

    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

It looks slightly different but it's testing the same thing. This is just suggestion, because I'm not sure what's going wrong.

Upvotes: 2

indiscrete
indiscrete

Reputation: 79

It looks like a couple of methods are missing (if you're trying to match the tutorial exactly) Listing 6.25 & 6.28:

describe "when password doesn't match confirmation" do before { @user.password_confirmation = "mismatch" } 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 be_invalid } end

Upvotes: 0

Related Questions