ruby-on-rails - RailsTutorial第8章错误的参数数量(1对2)

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

我陷入了第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)

但是您的测试却用两个调用它:
@user.authenticated?(:remember, '')

关于ruby-on-rails - RailsTutorial第8章错误的参数数量(1对2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33745275/

相关文章:

ruby-on-rails - 将 XML 转换为 Ruby 哈希时保留属性

c++ - 如何在C++中进行错误处理和捕获

ruby-on-rails - Rails 教程 : before_filter deprecated

ruby-on-rails - Rubymine 6 更新/刷新 rails 项目中的可用方法和路径?

ruby-on-rails - Rails HAML 引擎渲染

javascript - 如何处理 Promise 模式中异步调用之前发生的错误?

c# - 如何使用包含资源值的IController Execute()返回View?

ruby-on-rails - ZenTest 4.9.3 显示为无效的 gemspec

ruby-on-rails - Rails 教程 : Testing Error (Cannot Load Such File)

javascript - 你如何在客户端处理复杂的 JavaScript 对象?