前提・実現したいこと
ただいま、rails tutorialの11章を勉強中です
editアクションを使ったメールによるアカウント認証機能を
実装しようとしていました。
3点failuresが出ています。
アドバイス等、よろしくお願いします。
発生している問題・エラーメッセージ
Running:
.....F
Failure:
UsersControllerTest#test_should_redirect_update_when_logged_in_as_wrong_user [/home/ubuntu/environment/sample_app/test/controllers/users_controller_test.rb:40]:
Expected false to be truthy.
rails test test/controllers/users_controller_test.rb:36
F
Failure:
UsersControllerTest#test_should_redirect_destroy_when_logged_in_as_a_non-admin [/home/ubuntu/environment/sample_app/test/controllers/users_controller_test.rb:61]:
Expected response to be a redirect to http://www.example.com/ but was a redirect to http://www.example.com/login.
Expected "http://www.example.com/" to be === "http://www.example.com/login".
rails test test/controllers/users_controller_test.rb:56
F
Failure:
UsersControllerTest#test_should_redirect_edit_when_logged_in_as_wrong_user [/home/ubuntu/environment/sample_app/test/controllers/users_controller_test.rb:32]:
Expected false to be truthy.
rails test test/controllers/users_controller_test.rb:29
..............................
該当のソースコード
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
test "should redirect index when not logged in" do
get users_path
assert_redirected_to login_url
end
test "should redirect destroy when not logged in" do
assert_no_difference 'User.count' do
delete user_path(@user)
end
assert_redirected_to login_url
end
test "should redirect destroy when logged in as a non-admin" do
log_in_as(@other_user)
assert_no_difference 'User.count' do
delete user_path(@user)
end
assert_redirected_to root_url
end
end
試したこと
11章のテスト前のコードを書き直しましたがエラー解消されません。
補足情報(FW/ツールのバージョンなど)
使っているのはRails 6.0.3です。
下記に今回変更したコードを貼ります。
class AccountActivationsController < ApplicationController
def edit
user = User.find_by(email: params[:email])
if user && !user.activated? && user.authenticated?(:activation, params[:id])
user.activate
log_in user
flash[:success] = "Account activated!"
redirect_to user
else
flash[:danger] = "Invalid activation link"
redirect_to root_url
end
end
end
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
# 更新に成功した場合を扱う。
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
ログイン済みユーザーかどうか確認
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
正しいユーザーかどうか確認
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。