前提・実現したいこと
rails tutorial 11章11.3.3のアカウント有効化の確認のテストにてエラーが発生しており、修正部分が分からない状況です。assert user.reload.activated?でエラーが発生しているようです。
発生している問題・エラーメッセージ
FAIL["test_valid_signup_information_with_account_activation", UsersSignupTest, 1.4292580001056194] test_valid_signup_information_with_account_activation#UsersSignupTest (1.43s) Expected false to be truthy. test/integration/users_signup_test.rb:44:in `block in <class:UsersSignupTest>'
該当のソースコード
require 'test_helper' class UsersSignupTest < ActionDispatch::IntegrationTest def setup ActionMailer::Base.deliveries.clear end test "invalid signup information" do get signup_path assert_no_difference 'User.count' do post users_path, params: { user: { name: "", email: "user@invalid", password: "foo", password_confirmation: "bar" } } end assert_template 'users/new' assert_select 'div#error_explanation' assert_select 'div.field_with_errors' end test "valid signup information with account activation" do get signup_path assert_difference 'User.count', 1 do post users_path, params: { user: { name: "Example User", email: "user@example.com", password: "password", password_confirmation: "password" } } end assert_equal 1, ActionMailer::Base.deliveries.size user = assigns(:user) assert_not user.activated? # 有効化していない状態でログインしてみる log_in_as(user) assert_not is_logged_in? # 有効化トークンが不正な場合 get edit_account_activation_path("invalid token", email: user.email) assert_not is_logged_in? # トークンは正しいがメールアドレスが無効な場合 get edit_account_activation_path(user.activation_token, email: 'wrong') assert_not is_logged_in? # 有効化トークンが正しい場合 get edit_account_activation_path(user.activation_token, email: user.email) assert user.reload.activated? follow_redirect! assert_template 'users/show' assert is_logged_in? end end
session_controller class SessionsController < ApplicationController def new end def create user = User.find_by(email: params[:session][:email].downcase) if user && user.authenticate(params[:session][:password]) if user.activated? log_in user params[:session][:remember_me] == '1' ? remember(user) : forget(user) redirect_back_or user else message = "Account not activated. " message += "Check your email for the activation link." flash[:warning] = message redirect_to root_url end else flash.now[:danger] = 'Invalid email/password combination' render 'new' end end def destroy log_out if logged_in? redirect_to root_url end end
users_controller 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 new @user = User.new end def show @user = User.find(params[:id]) end def create @user = User.new(user_params) if @user.save UserMailer.account_activation(@user).deliver_now 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_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
試したこと
users_controller,session_controllerをtutorialの通りに修正しましたが改善されません。
補足情報(FW/ツールのバージョンなど)
Rails 5.1.7
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。