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

时间:2019-03-22 15:48:35

标签: ruby-on-rails ruby

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

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

以下是相关文件,非常感谢您的帮助

谢谢!

来自user.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

来自sessions_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

编辑:我将不再允许我发布任何代码,因此,如果您需要查看任何内容,请告诉我!

2 个答案:

答案 0 :(得分:0)

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

  

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

     

基于注释,您好像只是在没有has_secure_password的时候创建了用户,因此密码摘要从未存储。在数据库中查看,您可能会看到该用户的password_digest为空。从数据库中删除用户,然后使用您的新工作代码重新创建,它应该可以工作。

答案 1 :(得分:0)

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

我的test/fixtures/users.yml文件中出现noobie错误。

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

应该已经password_digest: <%= User.digest('password') %>,现在错误要消失了。