大家。我有以下问题: 实现has_password后?在7.2.3节中,RSpec为“应该创建一个给定有效属性的新实例”测试显示以下错误,例如
1)用户应该创建一个给定有效属性的新实例 失败/错误:User.create!(@ attr) 引发ArgumentError: 错误的参数个数(1表示0) #。/ app / model / user.rb:42:in
secure_hash' # ./app/models/user.rb:39:in
make_salt' './app/models/user.rb:30:inencrypt_password' # ./spec/models/user_spec.rb:14:in
阻止(2级)'在1.47秒内完成 1例,1次失败 < - Slave(1)运行完毕!
我不明白,究竟是什么导致了这个问题。 这是我的user.rb代码:
require 'digest'
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
# Automatically create the virtual attribute "password_confirmation"
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
before_save :encrypt_password
# Return 'true' if the user's password matches the submitted password
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
private
def encrypt_password
self.salt = make_salt if new_record?
self.encrypted_password = encrypt(password)
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end
def secure_hash
Digest::SHA2.hexdigest(string)
end
end
它能是什么? 提前谢谢!
答案 0 :(得分:2)
您的 secure_hash 方法需要参数。
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end