##問題
12.3.3 パスワードの再設定をテストするのテスト実行に失敗する。
##エラーの原因
PasswordResetsControllerTestで、302エラーが返ってくるため、テストに失敗する。
yokoyan:~/workspace/sample_app (password-reset) $ rails test
Running via Spring preloader in process 4508
Started with run options --seed 23235
FAIL["test_should_get_edit", PasswordResetsControllerTest, 1.433484478999162]
test_should_get_edit#PasswordResetsControllerTest (1.43s)
Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/>
test/controllers/password_resets_controller_test.rb:11:in `block in <class:PasswordResetsControllerTest>'
47/47: [=====================================================================================================================] 100% Time: 00:00:02, Time: 00:00:02
Finished in 2.42694s
47 tests, 205 assertions, 1 failures, 0 errors, 0 skips
##根本的な原因
12.1.1 PasswordResetsコントローラにて、作成しなくても良いテストを作成していたため。
(PasswordResetsControllerTestは作成しなくて良いテスト)
以下、実行したコマンド。
「--no-test-framework」と打つべきところを、「--no--test-framework」とタイプしているため、テストケースが作成されてしまっていた。
yokoyan:~/workspace/sample_app (password-reset) $ rails generate controller PasswordResets new edit --no--test-framework
Running via Spring preloader in process 1737
Expected string default value for '--jbuilder'; got true (boolean)
Expected string default value for '--helper'; got true (boolean)
Expected string default value for '--assets'; got true (boolean)
create app/controllers/password_resets_controller.rb
route get 'password_resets/edit'
route get 'password_resets/new'
invoke erb
create app/views/password_resets
create app/views/password_resets/new.html.erb
create app/views/password_resets/edit.html.erb
invoke test_unit
create test/controllers/password_resets_controller_test.rb
invoke helper
create app/helpers/password_resets_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/password_resets.coffee
invoke scss
create app/assets/stylesheets/password_resets.scss
##対策
不要なテストを消す。
rm test/controllers/password_resets_controller_test.rb
##再度テストを実行する
無事に動くようになりました。
めでたしめでたし。
yokoyan:~/workspace/sample_app (password-reset) $ rails test
Running via Spring preloader in process 4725
Started with run options --seed 25295
45/45: [==================================================================================================] 100% Time: 00:00:02, Time: 00:00:02
Finished in 2.21907s
45 tests, 203 assertions, 0 failures, 0 errors, 0 skips
##注意点
一度、rails generate
でPasswordResetsControllerTestを作成してしまった後に、
再度正しいオプションで、rails generate
をしても、PasswordResetsControllerTestは消えないので注意。むしろ他の必要なファイルがコンフリクトしまくりで被害が拡大。
yokoyan:~/workspace/sample_app (password-reset) $ rails generate controller PasswordResets new edit --no-test-framework
Running via Spring preloader in process 4624
Expected string default value for '--jbuilder'; got true (boolean)
Expected string default value for '--helper'; got true (boolean)
Expected string default value for '--assets'; got true (boolean)
Overwrite /home/ubuntu/workspace/sample_app/app/controllers/password_resets_controller.rb? (enter "h" for help) [Ynaqdh]
force app/controllers/password_resets_controller.rb
route get 'password_resets/edit'
route get 'password_resets/new'
invoke erb
exist app/views/password_resets
conflict app/views/password_resets/new.html.erb
Overwrite /home/ubuntu/workspace/sample_app/app/views/password_resets/new.html.erb? (enter "h" for help) [Ynaqdh] n
skip app/views/password_resets/new.html.erb
conflict app/views/password_resets/edit.html.erb
Overwrite /home/ubuntu/workspace/sample_app/app/views/password_resets/edit.html.erb? (enter "h" for help) [Ynaqdh] n
skip app/views/password_resets/edit.html.erb
invoke helper
identical app/helpers/password_resets_helper.rb
invoke assets
invoke coffee
identical app/assets/javascripts/password_resets.coffee
invoke scss
identical app/assets/stylesheets/password_resets.scss