def password=(password) @password = password if password.present? generate_salt self.hashed_password = self.class.encrypt_password(password,salt) end end
def User.authenticate(name,password) if user == find_by_name(name) if user.hashed_password == encrypt_password(password,user.salt) user end end end
private def password_must_be_present errors.add(:password,"密码错误")unless hashed_password.present? end
def User.encrypt_password(password,salt) Digest::SHA2.hexdigest(password + "wibble" + salt) end
def generate_salt self.salt= self.object_id.to_s + rand.to_s end end
这是模型部分代码,他的需求如下
1.name与password不得为nil
2.两次密码输入必须一致
3.name是唯一的
4..hashed_password与self不得暴露给用户
下面就是rspec写的测试代码
require 'spec_helper'
describe User do
it "正确参数" do user = FactoryGirl.build(:user1) assert user.valid? end
it "name值为空时,user对象应该无效" do user = FactoryGirl.build(:user2) assert user.invalid? end
it "password虚拟属性为空时,user对象应该无效" do user = Factory.build(:user5) user.should_not be_valid user.should have(1).errors_on(:password) end
it "password_confirmation虚拟属性为nil时,user对象应该无效" do user = Factory.build(:user_password_confirmation_nil) assert user.invalid? user.should have(1).errors_on(:password) end
it "两次密码不相同时,user对象应该无效" do user = FactoryGirl.build(:user3) assert user.invalid? end
it "当name已经存在时,对象不应该被创建并会报关于:name的错误" do fist_user = Factory.create(:user1) #factory用create创建表示还有save动作而用 build则没有save user = Factory.build(:user1) assert user.invalid? user.should have(1).errors_on(:name) #结果会有关于name一个错误。 end
end
上面就是rspec测试代码部分主要需要注意的是create与build创建时的不同
下面是factory构建的测试代码
FactoryGirl.define do factory :user1 ,:class =>:User do name "user1" hashed_password "MyString" salt "MyString" password "123" password_confirmation "123" end
factory :user2 ,:class =>:User do name "" hashed_password "MyString" salt "MyString" password "123" password_confirmation "123" end
factory :user3 ,:class =>:User do name "user3" hashed_password "MyString" salt "MyString" password "123" password_confirmation "1234" end
factory :user4 ,:class =>:User do name "user4" hashed_password "MyString" salt "MyString" end factory :user5 ,:class =>:User do name "user1" password "" password_confirmation "" end factory :user_password_confirmation_nil ,:class =>:User do name"user" password "123456" password_confirmation "" end