繁体   English   中英

Michael Hartl Ruby on Rails 教程第 9 章失败的测试

[英]Michael Hartl Ruby on Rails Tutorial Chapter 9 Failing Tests

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

我在运行 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

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

尝试登录时出现“BCrypt::Errors::InvalidHash”看起来与您遇到的问题相同:

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

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

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

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

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

它应该是password_digest: <%= User.digest('password') %>现在错误消失了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM