Michael Hartl 的教程第 10.2.2 章,列出 10.26 测试失败
Michael Hartl's tutorial Chapter 10.2.2, listing 10.26 tests failing
浏览 Hartl 的教程并搜索了答案,但找到了 none。 (可能存在答案,但由于章节已更新并在较新版本中切换,如果它们存在,我找不到它们。)
我在第 10.2.2 节。清单 10.26 正在执行 rails 测试。
这是我的测试错误:
ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 1.5802158990409225]
test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1.58s)
BCrypt::Errors::InvalidHash: BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:9:in `create'
test/test_helper.rb:32:in `log_in_as'
test/controllers/users_controller_test.rb:29:in `block in <class:UsersControllerTest>'
ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 1.606778411893174]
test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1.61s)
BCrypt::Errors::InvalidHash: BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:9:in `create'
test/test_helper.rb:32:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
35/35: [=====================================================] 100% Time: 00:00:01, Time: 00:00:01
Finished in 1.71823s
35 tests, 87 assertions, 0 failures, 2 errors, 0 skips
test/fixtures/users.yml
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
archer:
name: Sterling Archer
email: duchess@example.gov
password_digest: <%= User.digest('password') %
test/controllers/users_controller_test.rb
require 'test_helper'
class UsersControllerTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
@other_user = users(:archer)
end
test "should get new" do
get signup_path
assert_response :success
end
test "should redirect edit when not logged in" do
get edit_user_path(@user)
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect update when not logged in" do
patch user_path(@user), params: { user: { name: @user.name,
email: @user.email } }
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect edit when logged in as wrong user" do
log_in_as(@other_user)
get edit_user_path(@user)
assert flash.empty?
assert_redirected_to root_url
end
test "should redirect update when logged in as wrong user" do
log_in_as(@other_user)
patch user_path(@user), params: { user: { name: @user.name,
email: @user.email } }
assert flash.empty?
assert_redirected_to root_url
end
end
app/controllers/users_controller.rb
class UsersController < ApplicationController
before_action :logged_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
# @user = User.new(params[:user]) # Not the final implementation!
if @user.save
log_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
# Handle a successful save.
else
render 'new'
end
end
def edit
# can take out because already in correct_user @user = User.find(params[:id])
end
def update
# can take out because already in correct_user @user = User.find(params[:id])
if @user.update_attributes(user_params)
# Handle a successful update.
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless @user == current_user
end
end
为什么书上说所有测试都应该通过,但我却有 2 个失败的测试?我查看了错误消息中提到的行,但我不确定哪里出了问题。
感谢您的帮助。
编辑:会话控制器
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' # Not quite right!
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
仔细看行:
BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:9:in `create'
你没有指定这个控制器的代码,我不能告诉你可能有什么问题。
您在 password_digest
中缺少 "Sterling Archer" 测试夹具的右尖括号。
password_digest: <%= User.digest('password') %
应该是
password_digest: <%= User.digest('password') %>
浏览 Hartl 的教程并搜索了答案,但找到了 none。 (可能存在答案,但由于章节已更新并在较新版本中切换,如果它们存在,我找不到它们。)
我在第 10.2.2 节。清单 10.26 正在执行 rails 测试。
这是我的测试错误:
ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 1.5802158990409225]
test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1.58s)
BCrypt::Errors::InvalidHash: BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:9:in `create'
test/test_helper.rb:32:in `log_in_as'
test/controllers/users_controller_test.rb:29:in `block in <class:UsersControllerTest>'
ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 1.606778411893174]
test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1.61s)
BCrypt::Errors::InvalidHash: BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:9:in `create'
test/test_helper.rb:32:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
35/35: [=====================================================] 100% Time: 00:00:01, Time: 00:00:01
Finished in 1.71823s
35 tests, 87 assertions, 0 failures, 2 errors, 0 skips
test/fixtures/users.yml
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
archer:
name: Sterling Archer
email: duchess@example.gov
password_digest: <%= User.digest('password') %
test/controllers/users_controller_test.rb
require 'test_helper'
class UsersControllerTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
@other_user = users(:archer)
end
test "should get new" do
get signup_path
assert_response :success
end
test "should redirect edit when not logged in" do
get edit_user_path(@user)
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect update when not logged in" do
patch user_path(@user), params: { user: { name: @user.name,
email: @user.email } }
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect edit when logged in as wrong user" do
log_in_as(@other_user)
get edit_user_path(@user)
assert flash.empty?
assert_redirected_to root_url
end
test "should redirect update when logged in as wrong user" do
log_in_as(@other_user)
patch user_path(@user), params: { user: { name: @user.name,
email: @user.email } }
assert flash.empty?
assert_redirected_to root_url
end
end
app/controllers/users_controller.rb
class UsersController < ApplicationController
before_action :logged_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
# @user = User.new(params[:user]) # Not the final implementation!
if @user.save
log_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
# Handle a successful save.
else
render 'new'
end
end
def edit
# can take out because already in correct_user @user = User.find(params[:id])
end
def update
# can take out because already in correct_user @user = User.find(params[:id])
if @user.update_attributes(user_params)
# Handle a successful update.
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless @user == current_user
end
end
为什么书上说所有测试都应该通过,但我却有 2 个失败的测试?我查看了错误消息中提到的行,但我不确定哪里出了问题。 感谢您的帮助。
编辑:会话控制器
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' # Not quite right!
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
仔细看行:
BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:9:in `create'
你没有指定这个控制器的代码,我不能告诉你可能有什么问题。
您在 password_digest
中缺少 "Sterling Archer" 测试夹具的右尖括号。
password_digest: <%= User.digest('password') %
应该是
password_digest: <%= User.digest('password') %>