前提・実現したいこと
Ruby on Rails チュートリアル第10章の10.4.3 ユーザー削除テストで、リスト10.62の統合テストを実行したところエラーが1つ出てしまいます。
発生している問題・エラーメッセージ
ERROR["test_should_redirect_destroy_when_not_logged_in", UsersControllerTest, 1.0210249440001462] test_should_redirect_destroy_when_not_logged_in#UsersControllerTest (1.02s) NoMethodError: NoMethodError: undefined method `admin?' for nil:NilClass app/controllers/users_controller.rb:72:in `admin_user' test/controllers/users_controller_test.rb:65:in `block (2 levels) in <class:UsersControllerTest>' test/controllers/users_controller_test.rb:64:in `block in <class:UsersControllerTest>'
該当のソースコード
sample_app/app/controllers/users_controller.rbの内容
class UsersController < ApplicationController before_action :logged_in_user, only: [:index,:edit, :update] 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 log_in @user flash[:success] = "Welcome to the Sample APP!" redirect_to @user else render 'new' end end def edit end def update @user = User.find(params[:id]) if @user.update_attributes(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
sample_app/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 test "should redirect index when not logged in" do get users_path assert_redirected_to login_url end test "should not alllow the admin attribute to be edited via the web" do log_in_as(@other_user) assert_not @other_user.admin? patch user_path(@other_user), params:{ user: { password: "password", password_confirmation: "password", admin: true } } assert_not @other_user.reload.admin? 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
試したこと
一通り調べてみたのですがわかりませんでした。。。
回答1件
あなたの回答
tips
プレビュー