【发布时间】: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