RailsTutorial Chapter 8 参数个数错误(1 for 2)
RailsTutorial Chapter 8 wrong number of arguments (1 for 2)
我卡在第 8 章的末尾,收到以下错误消息:
test_authenticated?_should_return_false_for_a_user_with_nil_digest#UserTest ArgumentError: wrong number of arguments (2 for 1)
app/models/user.rb:36:in `authenticated?'
test/models/user_test.rb:65:in `block in '
教程说我应该通过它(绿色)。非常感谢您的帮助
user.rb file is:
class User < ActiveRecord::Base
attr_accessor :remember_token
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end
# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
# Forgets a user.
def forget
update_attribute(:remember_digest, nil)
end
# Returns true if the given token matches the digest.
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
end
user_test.rb is (64-66):
test "authenticated? should return false for a user with nil digest" do
assert_not @user.authenticated?(:remember, '')
end
您的 authenticated?
方法签名采用一个参数,从
可以明显看出
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
但是你传递的是 2。
下面这位...
test "authenticated? should return false for a user with nil digest" do
assert_not @user.authenticated?(:remember, '')
end
应该是
test "authenticated? should return false for a user with nil digest" do
assert_not @user.authenticated?(:remember)
end
您已将 authenticated?
方法定义为接受单个参数:
def authenticated?(remember_token)
然而你的测试用两个调用它:
@user.authenticated?(:remember, '')
我找到了正确答案 - 清单 8.43:
应该是
test "authenticated? should return false for a user with nil digest" do
assert_not @user.authenticated?('')
end
不要从 Michael Hartl 的 github 复制代码,网站和 repo 之间有太多不一致之处。
我卡在第 8 章的末尾,收到以下错误消息:
test_authenticated?_should_return_false_for_a_user_with_nil_digest#UserTest ArgumentError: wrong number of arguments (2 for 1)
app/models/user.rb:36:in `authenticated?'
test/models/user_test.rb:65:in `block in '
教程说我应该通过它(绿色)。非常感谢您的帮助
user.rb file is:
class User < ActiveRecord::Base
attr_accessor :remember_token
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end
# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
# Forgets a user.
def forget
update_attribute(:remember_digest, nil)
end
# Returns true if the given token matches the digest.
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
end
user_test.rb is (64-66):
test "authenticated? should return false for a user with nil digest" do
assert_not @user.authenticated?(:remember, '')
end
您的 authenticated?
方法签名采用一个参数,从
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
但是你传递的是 2。 下面这位...
test "authenticated? should return false for a user with nil digest" do
assert_not @user.authenticated?(:remember, '')
end
应该是
test "authenticated? should return false for a user with nil digest" do
assert_not @user.authenticated?(:remember)
end
您已将 authenticated?
方法定义为接受单个参数:
def authenticated?(remember_token)
然而你的测试用两个调用它:
@user.authenticated?(:remember, '')
我找到了正确答案 - 清单 8.43: 应该是
test "authenticated? should return false for a user with nil digest" do
assert_not @user.authenticated?('')
end
不要从 Michael Hartl 的 github 复制代码,网站和 repo 之间有太多不一致之处。