Note that there are some explanatory texts on larger screens.

plurals
  1. POpassword Ruby on Rails Tutorial by M. Hartl
    text
    copied!<p>I've just started using rails, and decided to follow the "<em>Ruby on Rails Tutorial</em>" by M. Hartl. Seems like a good intro.</p> <p><strong>Am running into a failed test</strong> that's driving me nuts. I am running <strong>rails 3.1.1</strong>, with <strong>rspec 2.7.0</strong></p> <p>I have tried modifying the condition, and tests on the "has_password" method work.</p> <p>The failing test:</p> <pre> 1) User password encryption authenticate method should return the user on email/password match Failure/Error: matching_user.should == @user expected: # got: nil (using ==) # ./spec/models/user_spec.rb:149:in `block (4 levels) in ' </pre> <p>The rspec test:</p> <pre><code>describe User do before(:each) do @attr = {:name =&gt; 'testing', :email =&gt;'testing@example.com', :password =&gt; "testtest", :password_confirmation =&gt; "testtest"} end ... describe "password encryption" do before(:each) do @user = User.create!(@attr) end ... describe "authenticate method" do it "should exist" do User.should respond_to(:authenticate) end it "should return nil on email/password mismatch" do User.authenticate(@attr[:email], "wrongpass").should be_nil end it "should return nil for an email address with no user" do User.authenticate("bar@foo.com", @attr[:password]).should be_nil end it "should return the user on email/password match" do matching_user = User.authenticate(@attr[:email], @attr[:password]) matching_user.should == @user end end </code></pre> <p>In the User model:</p> <pre><code>... def has_password?(submitted_password) encrypt_password == encrypt(submitted_password) end def self.authenticate(email, submitted_password) user = find_by_email(email) #self.where("email = ?", email) return nil if user.nil? return user if user.has_password?(submitted_password) end private def encrypt_password self.salt = make_salt if new_record? self.encrypted_password = encrypt(password) end </code></pre> <p>I cannot figure out what I'm doing wrong here.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload