** 前提・実現したいこと**
現在、Ruby on Rails Tutorial を写経しながら学習を進めています。
標題の節を進めているのですが、途中のテストでredになってしまいます。
原因がわかる方がいらっしゃいましたらご教示願います。
** 発生している問題・エラーメッセージ**
$ rails t
Running via Spring preloader in process 6077 Started with run options --seed 22425 ERROR["test_login_with_remembering", UsersLoginTest, 1.4186359340001218] test_login_with_remembering#UsersLoginTest (1.42s) NoMethodError: NoMethodError: undefined method `remember_token' for nil:NilClass test/integration/users_login_test.rb:43:in `block in <class:UsersLoginTest>' 42/42: [=============================================================================================================] 100% Time: 00:00:01, Time: 00:00:01 Finished in 1.50971s 42 tests, 176 assertions, 0 failures, 1 errors, 0 skips
NoMethodError: undefined method 'remember_token' for nil:NilClass
とあります。
第9章の演習 (9.3.1.1)でapp/controllers/sessions_controller.rb
内の通常のローカル変数user
をインスタンス変数@user
に変更していましたが、11章のリスト (11.32)で再びuser
に戻したことがこのエラーの原因かと思われます。
統合テストでは、仮想の`remember_token`属性にアクセスできない ↓ `assigns`という特殊なテストメソッドを使うとアクセスできるようになる ↓ `@user`というインスタンス変数が定義されていれば、テスト内部では`assigns(:user)`と書くことでインスタンス変数にアクセスできる。 という経緯で9章で変更していました。
なので、再び@user
に変更し、テストを行ったところ…
Running via Spring preloader in process 6799 Started with run options --seed 48516 ERROR["test_login_with_remembering", UsersLoginTest, 0.7189132280000194] test_login_with_remembering#UsersLoginTest (0.72s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x00000007645378> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/integration/users_login_test.rb:42:in `block in <class:UsersLoginTest>' ERROR["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 0.7384869200004687] test_login_with_valid_information_followed_by_logout#UsersLoginTest (0.74s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x007fde602f1598> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/integration/users_login_test.rb:21:in `block in <class:UsersLoginTest>' ERROR["test_login_without_remembering", UsersLoginTest, 0.7538774949998697] test_login_without_remembering#UsersLoginTest (0.75s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x000000078ad570> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/integration/users_login_test.rb:48:in `block in <class:UsersLoginTest>' ERROR["test_successful_edit", UsersEditTest, 0.7702211570003783] test_successful_edit#UsersEditTest (0.77s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x00000004319a30> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/integration/users_edit_test.rb:22:in `block in <class:UsersEditTest>' ERROR["test_successful_edit_with_friendly_forwarding", UsersEditTest, 0.8101292460005425] test_successful_edit_with_friendly_forwarding#UsersEditTest (0.81s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x000000078a9d80> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/integration/users_edit_test.rb:41:in `block in <class:UsersEditTest>' ERROR["test_unsuccessful_edit", UsersEditTest, 0.8249431140002343] test_unsuccessful_edit#UsersEditTest (0.83s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x000000076293d0> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/integration/users_edit_test.rb:10:in `block in <class:UsersEditTest>' ERROR["test_valid_signup_information_with_account_activation", UsersSignupTest, 1.1824065370001335] test_valid_signup_information_with_account_activation#UsersSignupTest (1.18s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x0000000951a5f0> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/integration/users_signup_test.rb:34:in `block in <class:UsersSignupTest>' ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 1.2318844230003378] test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1.23s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x0000000969f358> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/controllers/users_controller_test.rb:41:in `block in <class:UsersControllerTest>' ERROR["test_should_redirect_destroy_when_logged_in_as_a_non-admin", UsersControllerTest, 1.2460524620000797] test_should_redirect_destroy_when_logged_in_as_a_non-admin#UsersControllerTest (1.25s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x00000009797990> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/controllers/users_controller_test.rb:66:in `block in <class:UsersControllerTest>' ERROR["test_should_not_allow_the_admin_attribute_to_be_edited_via_the_web", UsersControllerTest, 1.2702050879997842] test_should_not_allow_the_admin_attribute_to_be_edited_via_the_web#UsersControllerTest (1.27s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x000000098d47e0> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/controllers/users_controller_test.rb:49:in `block in <class:UsersControllerTest>' ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 1.2845417730004556] test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1.28s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x000000099c9010> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/controllers/users_controller_test.rb:34:in `block in <class:UsersControllerTest>' ERROR["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 1.3651507889999266] test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (1.37s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x00000009cf3bf0> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/integration/users_index_test.rb:11:in `block in <class:UsersIndexTest>' ERROR["test_index_as_non-admin", UsersIndexTest, 1.3875819069999125] test_index_as_non-admin#UsersIndexTest (1.39s) NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x00000009cf7a48> Did you mean? @user app/controllers/sessions_controller.rb:9:in `create' test/test_helper.rb:29:in `log_in_as' test/integration/users_index_test.rb:28:in `block in <class:UsersIndexTest>' 42/42: [=============================================================================================================] 100% Time: 00:00:01, Time: 00:00:01 Finished in 1.39742s 42 tests, 70 assertions, 0 failures, 13 errors, 0 skips
エラーが増えてしまいました。
** 試したこと**
user
を @user
に変更するということだと思うのですが、どれを変更して良いのかがよく理解できていません。
補足情報(FW/ツールのバージョンなど)
AWS Cloud9
Rails 5.1.6
以上になります。
お分かりになる方がいらっしゃいましたら、何卒、宜しくお願い致します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。