yutokyokutyo / rebuild_sample_app

https://railstutorial.jp/chapters/static_pages?version=5.0#cha-static_pages
https://rebuildsampleapp.herokuapp.com/
5 stars 0 forks source link

Exercises-11.3.3 #27

Closed yutokyokutyo closed 7 years ago

yutokyokutyo commented 7 years ago
  1. リスト 11.35にあるactivateメソッドはupdate_attributeを2回呼び出していますが、これは各行で1回ずつデータベースへ問い合わせしていることになります。リスト 11.39に記したテンプレートを使って、update_attributeの呼び出しを1回のupdate_columns呼び出しにまとめてみましょう (これでデータベースへの問い合わせが1回で済むようになります)。また、変更後にテストを実行し、 greenになることも確認してください。
  2. 現在は、/usersのユーザーindexページを開くとすべてのユーザーが表示され、/users/:idのようにIDを指定すると個別のユーザーを表示できます。しかし考えてみれば、有効でないユーザーは表示する意味がありません。そこで、リスト 11.40のテンプレートを使って、この動作を変更してみましょう8。なお、ここで使っているActive Recordのwhereメソッドについては、13.3.3でもう少し詳しく説明します。
  3. ここまでの演習課題で変更したコードをテストするために、/users と /users/:id の両方に対する統合テストを作成してみましょう。
yutokyokutyo commented 7 years ago

user.ymlにinvalidのユーザーを書いてしまうとページネーションテストで引っかかってしまう。 打開策を考えねば...。

 FAIL["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 1.5785890580009436]
 test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (1.58s)
        Expected at least 1 element matching "a[href="/users/771240128"]", found 0..
        Expected 0 to be >= 1.
        test/integration/users_index_test.rb:18:in `block (2 levels) in <class:UsersIndexTest>'
        test/integration/users_index_test.rb:17:in `block in <class:UsersIndexTest>'
yutokyokutyo commented 7 years ago
yutokyokutyo commented 7 years ago

テスト通った!

% rails t                                                                                                                    (git)-[Exercises-11.3.3]
Running via Spring preloader in process 85612
Started with run options --seed 23218

  44/44: [=======================================================================================================] 100% Time: 00:00:04, Time: 00:00:04

Finished in 4.31082s
44 tests, 193 assertions, 0 failures, 0 errors, 0 skips

@komukomo こちらレビューをお願いします 🙏 ✨

komukomo commented 7 years ago

activated:falseを表示するように戻した時にREDになることも確認してみましょう。 このときbaikinmanは1ページ目にきているでしょうか?(where句がない状態でbaikinmanが1ページ目にきていない場合、where句を書かなくてもGREENになっていまいます。) ユーザの順序はどうなっていますか? 順序の指定方法は後のセクションで学ぶので今は確認だけで大丈夫です。 (railstutorial風)

これが確認できたらOKだと思います!リダイレクトのテストもいいと思います!

yutokyokutyo commented 7 years ago

railstutorial風だw よーし!

% git diff                                                                                                                   (git)-[Exercises-11.3.3]
diff --git a/test/integration/users_index_test.rb b/test/integration/users_index_test.rb
index e4ab7e4..39976b6 100644
--- a/test/integration/users_index_test.rb
+++ b/test/integration/users_index_test.rb
@@ -13,7 +13,8 @@ class UsersIndexTest < ActionDispatch::IntegrationTest
     get users_path
     assert_template 'users/index'
     assert_select 'div.pagination'
-    first_page_of_users = User.where(activated: true).paginate(page: 1)
+    first_page_of_users = User.paginate(page: 1)
+    binding.pry
     first_page_of_users.each do |user|
       assert_select 'a[href=?]', user_path(user), text: user.name
       unless user == @admin

テスト実行すると失敗する。

 FAIL["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 64.81731915098499]
 test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (64.82s)
        Expected at least 1 element matching "a[href="/users/771240128"]", found 0..
        Expected 0 to be >= 1.
        test/integration/users_index_test.rb:19:in `block (2 levels) in <class:UsersIndexTest>'
        test/integration/users_index_test.rb:18:in `block in <class:UsersIndexTest>'

  44/44: [=======================================================================================================] 100% Time: 00:01:08, Time: 00:01:08

Finished in 68.09224s
44 tests, 185 assertions, 1 failures, 0 errors, 0 skips

pryが起動させてコーンソール上で確認すると Baikin Man いた!

[2] pry(#<UsersIndexTest>)> User.paginate(page: 1).find_by(name: 'Baikin Man')
=> #<User:0x007f9a5a0e2dc8
 id: 771240128,
 name: "Baikin Man",
 email: "baikinman@example.gov",
 created_at: Thu, 27 Apr 2017 00:16:59 UTC +00:00,
 updated_at: Thu, 27 Apr 2017 00:16:59 UTC +00:00,
 password_digest: "$2a$04$KEKbLt5W3fS2fBnKqt1vK.KGasbo0gC3ymUkP8tBm7VgvP01uxfYq",
 remember_digest: nil,
 admin: false,
 activation_digest: nil,
 activated: false,
 activated_at: Thu, 27 Apr 2017 00:16:59 UTC +00:00>

で、変更点を元に戻すとテストがGREEN!良さそう!

% rails t                                                                                                                    (git)-[Exercises-11.3.3]
Running via Spring preloader in process 35293
Started with run options --seed 21412

  44/44: [=======================================================================================================] 100% Time: 00:00:04, Time: 00:00:04

Finished in 4.59954s
yutokyokutyo commented 7 years ago

@komukomo 確認できたのでマージします!ありがとうございました ❤️

yutokyokutyo commented 7 years ago

スーパー学びを得たExercisesでした!