RailsTutorial第8章参数个数错误(1个为2)

时间:2015-11-16 21:49:18

标签: ruby-on-rails error-handling railstutorial.org

我已经陷入了第8章的末尾,我收到了以下错误消息:

  

test_authenticated?_should_return_false_for_a_user_with_nil_digest#UserTest ArgumentError:参数个数错误(2个为1)

     

app / models / user.rb:36:在`authenticated?'

     

test / models / user_test.rb:65:在`block in'

教程说我应该通过它(绿色)。非常感谢您的帮助

  

user.rb文件是:

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是(64-66):

test "authenticated? should return false for a user with nil digest" do
        assert_not @user.authenticated?(:remember, '')
end

3 个答案:

答案 0 :(得分:0)

您的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

答案 1 :(得分:0)

您已定义authenticated?方法以接受单个参数:

def authenticated?(remember_token)

然而你的测试是用两个来调用它:

@user.authenticated?(:remember, '')

答案 2 :(得分:0)

我找到了正确答案 - 清单8.43: 应该是

test "authenticated? should return false for a user with nil digest" do
    assert_not @user.authenticated?('')
end

不要从Michael Hartl的github复制代码,网站和回购之间存在太多不一致。