簡體   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