LoginSignup
0
0

More than 1 year has passed since last update.

minitestでエラー`block (2 levels) in <class:UsersIndexTest>'

Last updated at Posted at 2022-06-20

はじめに

minitestでテストを書いていると、
下記の部分でfailになった。

ターミナル
FAIL["test_index_as_admin_including_pagination_and_delete_links", #<Minitest::Reporters::Suite:0x00007fb558cdba80 @name="UsersIndexTest">, 2.8298179999983404]
 test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (2.83s)
Expected at least 1 element matching "a[href="/users/14035331"]", 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 `each'
        test/integration/users_index_test.rb:18:in `block in <class:UsersIndexTest>'

aタグのリンクが見つからないみたい。
だがどこを直せば良いか一見わからなかった。
ちなみにテストはこんな感じ。

test/integration/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 'nav.pagination', count: 2
    # ここがおかしかった。理由は後述。
        users = User.includes(:followers).sort {|a,b| b.followers.size <=> a.followers.size}
    first_page_of_users = Kaminari.paginate_array(users).page(1)
    first_page_of_users.each do |user|
      assert_select 'a[href=?]', user_path(user), text: user.name
      assert         user.activated
      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変数に入れるコードを途中で変えていたところがおかしかった。理由は後述する。

と言うわけで、いろんなとこをコメントアウトしながら切り分けをおこなった。

原因

「activatedがtrueになっている集合をデフォルトのid番号順」にusersに入れていたのを、
「followerが多い順」にソートした集合を入れるように変更していた。

app/controllers/users_controllers.rb
# users = User.where(activated: true) を下記に変えていた
  users = User.includes(:followers).sort {|a,b| b.followers.size <=> a.followers.size}

その際に、activatedについての記述を消してしまっていた。それによって一覧に表示されない人についてのデータも引っ張ってきてしまっていた。
従って、データだけははあるけど一覧表示されてないよ、aタグがないよ!
と言われてしまったわけだ。

結論

activatedがtrueの集合からソートを行うことで問題を解決できる。

app/controllers/users_controllers.rb
users = User.where(activated: true).includes(:followers).sort {|a,b| b.followers.size <=> a.followers.size}
test/integration/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 'nav.pagination', count: 2
    # ここのusers変数の式を書き換えた。
        users = User.where(activated: true).includes(:followers).sort {|a,b| b.followers.size <=> a.followers.size}
    first_page_of_users = Kaminari.paginate_array(users).page(1)
    first_page_of_users.each do |user|
      assert_select 'a[href=?]', user_path(user), text: user.name
      assert         user.activated
      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

おわりに

そろそろminitestからRspecに切り替えるか。。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0