質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

673閲覧

rails tutorial 11章におけるリダイレクト先が違うエラーとFLASH表示が違うエラー

YuMiya

総合スコア0

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/07/24 01:27

前提・実現したいこと

ただいま、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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

解決しました。原因はデータベースに不備でした。コードは変えていませんが
$ rails db:migrate:reset
$ rails db:seed
でエラーが発生せずにテストが通りました。

投稿2020/07/25 14:47

YuMiya

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問