【问题标题】:Michael Hartl Ruby on Rails Tutorial Chapter 9 Failing TestsMichael Hartl Ruby on Rails 教程第 9 章失败的测试
【发布时间】:2020-10-17 21:03:21
【问题描述】:

我试图寻找这个问题的答案,但无济于事。

我在运行 rails test 时收到此错误消息。

    1) Error:
UsersLoginTest#test_login_with_valid_information_followed_by_logout:
ActionView::Template::Error: invalid hash
    app/models/user.rb:32:in `new'
    app/models/user.rb:32:in `authenticated?'
    app/helpers/sessions_helper.rb:21:in `current_user'
    app/helpers/sessions_helper.rb:30:in `logged_in?'
    app/views/layouts/_header.html.erb:8:in `_app_views_layouts__header_html_erb__454288832_69555624'
    app/views/layouts/application.html.erb:9:in `_app_views_layouts_application_html_erb___219463369_68483868'
    test/integration/users_login_test.rb:45:in `block in <class:UsersLoginTest>'

24 runs, 66 assertions, 0 failures, 1 errors, 0 skips

这里是相关文件,任何帮助将不胜感激

谢谢!

来自用户.rb

  # 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

  # Returns true if the given token matches the digest.
  def authenticated?(remember_token)
    BCrypt::Password.new(remember_digest).is_password?(remember_token)
  end

  # Forgets a user
  def forget
    update_attribute(:remember_digest, nil)
  end

来自 session_helper.rb

  def current_user
    if (user_id = session[:user_id])
      @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.signed[:user_id])
      user = User.find_by(id: user_id)
      if user && user.authenticated?(cookies[:remember_token])
        log_in user
        @current_user = user
      end
    end
  end

  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end
require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end

  test "login with invalid information" do
    get login_path
    assert_template 'sessions/new'
    post login_path, params: { session: { email: "", password: ""} }
    assert_template 'sessions/new'
    assert_not flash.empty?
    get root_path
    assert flash.empty?
  end

  test "login with valid information" do
    get login_path
    post login_path, params: { session: { email:    @user.email,
                                          password: 'password' } }
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
  end

  test "login with valid information followed by logout" do
    get login_path
    post login_path, params: { session: { email:    @user.email,
                                          password: 'password' } }
    assert is_logged_in?
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    assert_not is_logged_in? 
    assert_redirected_to root_url
    follow_redirect!
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(@user), count: 0
  end
end

编辑:我不允许任何人发布更多代码,所以如果您需要查看任何内容,请告诉我!

【问题讨论】:

  • 在发布您的问题之前,您是否阅读过任何 69 search results 中的同一本书的完全相同的章节?如果您在帖子中包含您从这些重复的问题和答案中尝试过的事情,那就太好了,这样任何回答您的问题的人都不会给您一个您已经尝试过但未能正常工作的答案。我在您的帖子中根本没有看到任何关于您自己解决此问题所做的工作。

标签: ruby-on-rails ruby


【解决方案1】:

"BCrypt::Errors::InvalidHash" when trying to sign in 看起来与您遇到的问题相同:

这意味着存储在 password_digest 中的哈希不是有效的 BCrypt 哈希(包括如果该字段为空)。

根据 cmets,看起来您只是在 has_secure_password 不存在时创建了用户,因此密码摘要从未被存储。查看数据库,您可能会看到该用户的 password_digest 为空。从数据库中删除用户并使用新的工作代码重新创建它应该可以工作。

【讨论】:

  • 我在尝试注销时收到错误消息。当我检查控制台时,我看到生成了一个 password_digest 值,但我也看到 remember_digest 为 nil。不确定这是否是手头的问题
  • 您是否在包含 has_secure_password 后创建了用户?如果是这样尝试另一个用户,如果错误只发生在第一个用户身上,问题是他们的密码摘要。重新创建该用户应该可以解决问题
  • 抱歉,我刚刚看到这是在规范中发生的——您能否在规范中包含您生成用户的位置?这可能是一个问题
  • 刚刚添加了测试文件
  • 所以我只是看了一下教程的第 9 章(是的,这是周五下午的工作)
【解决方案2】:

我在学习教程时也遇到了这个问题(测试失败)。

我的test/fixtures/users.yml 文件中有一个小错误。

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <% User.digest('password') %>

应该是password_digest: &lt;%= User.digest('password') %&gt;,现在错误消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多