gpt4 book ai didi

ruby-on-rails - 失败| SessionsHelperTest#test_current_user_returns_right_user_when_session_is_nil

转载 作者:行者123 更新时间:2023-12-02 22:57:52 27 4
gpt4 key购买 nike

这次测试失败真是要了我的命。我正在关注railstutorial.org 电子书。我已经尽可能多地研究了这个失败。我一遍又一遍地阅读有关测试的内容,但我只是错过了一些东西,并且我已经在这个问题上停留了近一周。我已经无计可施了。感谢您的任何见解。

rake 测试结果:

  1) Failure:
SessionsHelperTest#test_current_user_returns_right_user_when_session_is_nil [/Users/test_user/app/test/helpers/sessions_helper_test.rb:11]:

--- expected
+++ actual
@@ -1 +1 @@
-#<User id: 762146111, first_name: "Michael", middle_name: "James", last_name: "Example", suffix: "Jr", created_at: "2015-08-01 14:13:26", updated_at: "2015-08-01 14:13:27", email: "michael@example.com", password_digest: "$2a$04$.AOr7J8q/Ft7Gu3HTNWjDet5kU2O8uFwV9l...", remember_digest: "$2a$04$./yszAudU5o/tovax413r.poDLg5N33a0XD...">
+nil

用户.rb

class User < ActiveRecord::Base

attr_accessor :remember_token

before_save { self.email = email.downcase }

validates :first_name, presence: true, length: { maximum: 20 }
validates :last_name, presence: true, length: { maximum: 30 }

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 }

validates :password, presence: true, length: { minimum: 6 }

has_secure_password


# 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 url-safe random string of 20 characters, each character having 64 possibilities
def User.new_token
SecureRandom.urlsafe_base64
end

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)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end

def forget
update_attribute(:remember_digest, nil)
end

end

sessions_controller.rb

class SessionsController < ApplicationController

def new
end

def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end

def destroy
log_out if logged_in?
redirect_to root_url
end

end

sessions_helper.rb

  module SessionsHelper

# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end

def remember(user)
user.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end

def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end

# Returns the current logged-in user (if any).
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

def log_out
forget(current_user)
session.delete(:user_id)
@current_user = nil
end

end

sessions_helper_test.rb

require 'test_helper'

class SessionsHelperTest < ActionView::TestCase

def setup
@user = users(:michael)
remember(@user)
end

test "current_user returns right user when session is nil" do
assert_equal @user, current_user
assert is_logged_in?
end

test "current_user returns nil when remember digest is wrong" do
@user.update_attribute(:remember_digest, User.digest(User.new_token))
assert_nil current_user
end
end

用户.yml

michael:
first_name: Michael
middle_name: James
last_name: Example
suffix: Jr
email: michael@example.com
password_digest: <%= User.digest('password') %>

最佳答案

失败的测试对我来说毫无意义:用户从未登录过,并且测试仍然期望 current_user 返回用户。

尝试以下 2 个测试:

test "current_user returns right user when session is set" do
log_in @user
assert_equal @user, current_user
assert is_logged_in?
end

test "current_user returns no user when session is nil" do
assert_nil current_user
assert !is_logged_in?
end

关于ruby-on-rails - 失败| SessionsHelperTest#test_current_user_returns_right_user_when_session_is_nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763060/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com