So I''m trying to be a good agile programmer and write unit tests for
my objects, but I''ve got a test I just can''t keep from
failing.
I have a profile object that I just added username and
hashed_password to. I used the method in the Rails book to handle
hashing the password. My code is strait out of the book. The whole
object is at the end of this email.
I want to test to make sure the hashed_password gets written out to
the database, so I wrote this test
def test_password_hash
@hash_pass = "something"
@profile_ron.password = @hash_pass
assert @profile_ron.save
@profile_ron.reload
assert_equal Digest::SHA1.hexdigest( @hash_pass ),
@profile_ron.hashed_password
end
The problem is that @profile_ron.hashed_password never changes. I''ve
put a bunch of logging and debugging code in and the problem actually
seems to be in the before_create method. After the assign,
self.hashed_password is nil. The hash_password method calculates the
hash, but we get nil back.
Any ideas what I''m doing wrong?
I haven''t tried the log in method yet, I wanted the hash test to work
first.
require "digest/sha1"
class Profile < ActiveRecord::Base
validates_presence_of :name, :email, :description, :addr_city, :addr_sta
te, :addr_zip, :username
attr_accessor :password
#================================ # Hash the password before save
#================================ def before_create
self.hashed_password = hash_password( self.password )
end
#================================ # Display the pseudonym if there is
one
#================================ def display_name
if pseudonym and !pseudonym.empty?
pseudonym
else
name
end
end
#================================ # login
#================================ def self.login(name, password)
hashed_password = hash_password(password || "")
find(:first,
:conditions => ["username = ? and
hashed_password = ?", username, hashed_password])
end
def try_to_login
Profile.login(self.username, self.password)
end
protected
def hash_password( inPassword )
Digest::SHA1.hexdigest(inPassword)
end
end
Ron Davis
I''ve got images in my head and I want to get them into my camera and
portfolio.
http://photo.reactuate.com/
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails