前提・実現したいこと
Railsリュートリアルを学んでいます。
第12章の最初の演習にてテストを行った結果、GREENになるはずがテストが通りませんでした。
管理者がユーザー一覧ページを見た時にpaginationクラスのdivがページ内に存在しない、という意味のエラーだと思うのですが、実際にrails s
でサーバーを立ててChromeにて管理者で一覧ページを見ると、ページ内に上記のdivは存在していました。
なぜテストではdivを認識してくれないのでしょうか。
教えていただけると幸いです。
発生している問題・エラーメッセージ
FAIL["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 4.460451300023124] test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (4.46s) Expected at least 1 element matching "div.pagination", found 0.. Expected 0 to be >= 1. test/integration/users_index_test.rb:17:in `block in <class:UsersIndexTest>'
該当のソースコード
#users_index_test.rb test "index as admin including pagination and delete links" do log_in_as(@admin) get users_path assert_template 'users/index' assert_select 'div.pagination' #この行でエラーが出ている first_page_of_users = User.paginate(page: 1) first_page_of_users.each do |user| assert_select 'a[href=?]', user_path(user), text: user.name unless user == @admin assert_select 'a[href=?]', user_path(user), text: 'delete' end end assert_difference 'User.count', -1 do delete user_path(@non_admin) end end
#users_controller.rb def index # @users = User.paginate(page: params[:page]) #演習11.3.2ではこの行を下の行のように改変した @users = User.where(activated: true).paginate(page: params[:page]) end
試したこと
演習11.3.2でユーザー一覧ページには認可済みのユーザーのみを表示するようにするためにusers_controller.rb
のindex
メソッドを上記のように改変しました。
この改変を行った結果テストが通らなくなることは分かりました。
フィクスチャにて認可済みユーザーを30人以上作成しているので、人数不足により次のページに進むリンクが存在しない、ということは無いと思います。
補足情報(FW/ツールのバージョンなど)
Ruby 2.5.5p157 (2019-03-15 revision 67260) [x64-mingw32]
Rails 5.1.7
will_paginate (3.1.6)
bootstrap-will_paginate (1.0.0)
あなたの回答
tips
プレビュー