前提
railsのminitestで、ページネーションが画面に表示されているかどうかのテストをしています。
コントローラーでユーザーの検索をかける処理で、
@users = User.paginate(page: params[:page])
から
@users = User.where(activated: true).paginate(page: params[:page])
と、アカウントを有効化しているユーザーのみを表示させようとwhereを使ったら「Expected exactly 2 elements matching "div.pagination", found 0..」というエラーが出ました。
ブラウザではページネーションは表示されていて、div.paginationもありました。
実現したいこと
・原因はなんなのかを知りたい
・エラーを無くすにはどうすればいいのか
発生している問題・エラーメッセージ
Expected exactly 2 elements matching "div.pagination", found 0..
該当のソースコード
controller
1 def index 2 @users = User.where(activated: true).paginate(page: params[:page]) 3 4 #成功 @users = User.paginate(page: params[:page]) 5 end
index.html.erb
1<% provide(:title, 'All users') %> 2<h1>All users</h1> 3 4<%= will_paginate %> 5 6<ul class="users"> 7 <%= render @users %> 8</ul> 9 10<%= will_paginate %>
test
1 test "index as admin including pagination and delete links" do 2 log_in_as(@admin) 3 get users_path 4 assert_template "users/index" 5 assert_select "div.pagination", count: 2 #ここがエラー 6 first_page_of_users = User.paginate(page: 1) 7 first_page_of_users.each do |user| 8 assert_select 'a[href=?]', user_path(user), text: user.name 9 unless user == @admin 10 assert_select "a[href=?]", user_path(user), text: "delete" 11 end 12 end 13 assert_difference 'User.count', -1 do 14 delete user_path(@non_admin) 15 end 16 end
試したこと
・whereの有無で試し、whereを記述するとエラーが起こりました。
・index.html.erbの「<%= will_paginate %>」を「<div class='pagination'><%= will_paginate %></div>」とするとテストは通りました。
補足情報(FW/ツールのバージョンなど)
cloud9
Rails 6.0.4

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。