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

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

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

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

Q&A

解決済

1回答

985閲覧

【Railsチュートリアル】Test: Expected true to be nil or falseを解決したい。

yt10

総合スコア12

Ruby on Rails 5

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

0グッド

1クリップ

投稿2019/02/08 06:00

編集2019/02/08 06:06

前提・実現したいこと

railsチュートリアル(第4版)11章のrails testを行なった結果、以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

ec2-user:~/environment/sample_app (account-activation) $ rails test Running via Spring preloader in process 13838 Started with run options --seed 10009 FAIL["test_valid_signup_information_with_account_activation", UsersSignupTest, 1.2656591489994753] test_valid_signup_information_with_account_activation#UsersSignupTest (1.27s) Expected true to be nil or false test/integration/users_signup_test.rb:35:in `block in <class:UsersSignupTest>' 33/33: [==========================================================================================] 100% Time: 00:00:01, Time: 00:00:01 Finished in 1.37386s 33 tests, 144 assertions, 1 failures, 0 errors, 0 skips

test/integration/users_signup_test.rb

Ruby

1require 'test_helper' 2 3class UsersSignupTest < ActionDispatch::IntegrationTest 4 5 def setup 6 ActionMailer::Base.deliveries.clear 7 end 8 9 test "invalid signup information" do 10 get signup_path 11 assert_no_difference 'User.count' do 12 post users_path, params: { user: { name: "", 13 email: "user@invalid", 14 password: "foo", 15 password_confirmation: "bar" } } 16 end 17 assert_template 'users/new' 18 assert_select 'div#error_explanation' 19 assert_select 'div.field_with_errors' 20 end 21 22 test "valid signup information with account activation" do 23 get signup_path 24 assert_difference 'User.count', 1 do 25 post users_path, params: { user: { name: "Example User", 26 email: "user@example.com", 27 password: "password", 28 password_confirmation: "password" } } 29 end 30 assert_equal 1, ActionMailer::Base.deliveries.size 31 user = assigns(:user) 32 assert_not user.activated? 33 # 有効化していない状態でログインしてみる 34 log_in_as(user) 35 assert_not is_logged_in? 36 # 有効化トークンが不正な場合 37 get edit_account_activation_path("invalid token", email: user.email) 38 assert_not is_logged_in? 39 # トークンは正しいがメールアドレスが無効な場合 40 get edit_account_activation_path(user.activation_token, email: 'wrong') 41 assert_not is_logged_in? 42 # 有効化トークンが正しい場合 43 get edit_account_activation_path(user.activation_token, email: user.email) 44 assert user.reload.activated? 45 follow_redirect! 46 assert_template 'users/show' 47 assert is_logged_in? 48 end 49end

###app/models/user.rb

Ruby

1class User < ApplicationRecord 2 attr_accessor :remember_token, :activation_token 3 before_save :downcase_email 4 before_create :create_activation_digest 5 validates :name, presence: true, length: { maximum: 50 } 6 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 7 validates :email, presence: true, length: { maximum: 255 }, 8 format: { with: VALID_EMAIL_REGEX }, 9 uniqueness: { case_sensitive: false } 10 has_secure_password 11 validates :password, presence: true, length: { minimum: 6 }, allow_nil: true 12 13 # 渡された文字列のハッシュ値を返す 14 def User.digest(string) 15 cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 16 BCrypt::Engine.cost 17 BCrypt::Password.create(string, cost: cost) 18 end 19 20 # ランダムなトークンを返す 21 def User.new_token 22 SecureRandom.urlsafe_base64 23 end 24 25 # 永続セッションのためにユーザーをデータベースに記憶する 26 def remember 27 self.remember_token = User.new_token 28 update_attribute(:remember_digest, User.digest(remember_token)) 29 end 30 31 32 # ユーザーのログイン情報を破棄する 33 def forget 34 update_attribute(:remember_digest, nil) 35 end 36 37 38 # アカウントを有効にする 39 def activate 40 update_attribute(:activated, true) 41 update_attribute(:activated_at, Time.zone.now) 42 end 43 44 # 有効化用のメールを送信する 45 def send_activation_email 46 UserMailer.account_activation(self).deliver_now 47 end 48 49 50 # トークンがダイジェストと一致したらtrueを返す 51 def authenticated?(attribute, token) 52 digest = send("#{attribute}_digest") 53 return false if digest.nil? 54 BCrypt::Password.new(digest).is_password?(token) 55 end 56 57 58 private 59 60 # メールアドレスをすべて小文字にする 61 def downcase_email 62 self.email = email.downcase 63 end 64 65 # 有効化トークンとダイジェストを作成および代入する 66 def create_activation_digest 67 self.activation_token = User.new_token 68 self.activation_digest = User.digest(activation_token) 69 end 70end

###routes.rb

Ruby

1Rails.application.routes.draw do 2 root 'static_pages#home' 3 get '/help', to: 'static_pages#help' 4 get '/about', to: 'static_pages#about' 5 get '/contact', to: 'static_pages#contact' 6 get '/signup', to: 'users#new' 7 get '/login', to: 'sessions#new' 8 post '/login', to: 'sessions#create' 9 delete '/logout', to: 'sessions#destroy' 10 resources :users 11 resources :account_activations, only: [:edit] 12end

試したこと

11章まで一通り見直してみましたが、同じエラーが発生してしまいました。

エラーの原因または解決策をご存知のかたがいれば、是非教えていただきたいです。

補足情報(FW/ツールのバージョンなど)

・railsチュートリアル第4版(Rails5.1)
・mac OS:Mojave 10.14.2

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

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

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

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

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

guest

回答1

0

自己解決

users_controller.rbのcreateアクションにlog_in @userを消し忘れていたことが原因でした。

問題があったusers_controller.rbのcreateアクション

Ruby

1 def create 2 @user = User.new(user_params) 3 if @user.save 4 @user.send_activation_email 5 log_in @user 6 flash[:success] = "Welcome to the Sample App!" 7 redirect_to root_url 8 else 9 render 'new' 10 end 11 end

Githubのrailsチュートリアルから添付した正しいusers_controller.rbのcreateアクション

Ruby

1 def create 2 @user = User.new(user_params) 3 if @user.save # => Validation 4 # Sucess 5 @user.send_activation_email 6 flash[:info] = "Please check your email to activate your account." 7 redirect_to root_url 8 else 9 # Failure 10 render 'new' 11 end 12 end

投稿2019/02/08 06:47

編集2019/02/08 07:00
yt10

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問