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

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

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

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 5

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

Ruby

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

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

780閲覧

テストを成功させたい

yuya-2002

総合スコア18

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 5

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

Ruby

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

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

1クリップ

投稿2020/09/07 08:56

出ているエラー・解決したい問題

オリジナルアプリを作成しており、rails tutorial11章リスト11.34でテストが通るはずだが、通らないので解決法を知りたい。

エラー内容

Running via Spring preloader in process 4526 Started with run options --seed 42509 FAIL["test_valid_signup_information_with_account_activation", UsersSignupTest, 1.2089103539999542] test_valid_signup_information_with_account_activation#UsersSignupTest (1.21s) Expected false to be truthy. test/integration/users_signup_test.rb:44:in `block in <class:UsersSignupTest>'

関連ファイル

test/integration/users_signup_test.rb

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

users_controller.rb

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 show @user = User.find(params[:id]) end def new @user = User.new end def create @user = User.new(user_params) if @user.save UserMailer.account_activation(@user).deliver_now flash[:info] = "メールに送られたURLをクリックしてくれ!" redirect_to root_url else render 'new' end end def edit end def update if @user.update_attributes(user_params) flash[:success] = "プロフィールを更新しました!" redirect_to @user else render 'edit' end end def destroy User.find(params[:id]).destroy flash[:success] = "ユーザーを削除しました" redirect_to users_url end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end # beforeアクション # ログイン済みユーザーかどうか確認 def logged_in_user unless logged_in? store_location flash[:danger] = "ログインしてくれ" 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

seddion.controler.rb

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 = "アカウントが認められませんでした!" message += "Check your email for the activation link." flash[:warning] = message redirect_to root_url end else flash.now[:danger] = 'メールアドレスかパスワードが違います!' render 'new' end end def destroy log_out if logged_in? redirect_to root_url end end

account_activations_controller.rb

class AccountActivationsController < ApplicationController def edit user = User.find_by(email: params[:email]) if user && !user.activated? && user.authenticated?(:activation, params[:id]) user.update_attribute(:activated, true) user.update_attribute(:activated_at, Time.zone.now) log_in user flash[:success] = "アカウントが認められました!" redirect_to user else flash[:danger] = "Invalid activation link" redirect_to root_url end end end

使用しているツール

rails5.1.6、ruby2.6.3、AWS、cloud9

いろいろ確認したが、どこが間違っているが分からないので、アドバイスを頂けるとありがたいです。
御教示よろしくお願いいします!

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

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

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

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

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

guest

回答1

0

ベストアンサー

いろいろ確認したが、どこが間違っているが分からないので

エラーメッセージは

Expected false to be truthy

なので、trueのはずなのにfalseになっているのでテストが通らない感じです。

エラーは出てるのは

test/integration/users_signup_test.rb:44

なのでおそらく

ruby

1assert user.reload.activated?

の部分で user.activeted? がtrueであってほしいけどfalseになっている感じです。

ruby

1get edit_account_activation_path(user.activation_token, email: user.email)
  • この部分で本当にuserが有効化されるか
  • user.activated? メソッドがおかしくないか

を確認すればエラーが解消できると思います。

投稿2020/09/07 10:44

SibakenY

総合スコア63

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

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

yuya-2002

2020/09/08 06:03

Shibaken Yさん 回答ありがとうございます! userが有効化されているか確認する方法は何かありますかね?
SibakenY

2020/09/08 11:50 編集

一番簡単なのは p assert user.reload.activated? とすればログにtrueかfalseが表示されると思います。 もしくは 確認したい場所、今回の場合だと assert user.reload.activated? の直前に debugger と記載すればデバッガーが起動すると思います。 そこで user.reload.activated? を打って確認することもできると思います。 またデバッガーを終了する際は continue と入力すればOKです。
yuya-2002

2020/09/09 05:22

ありがとうございます!
yuya-2002

2020/09/09 08:23

解決しました!ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問