0
0

More than 1 year has passed since last update.

railsチュートリアル第10章 ユーザー一覧のテスト

Posted at

ユーザー一覧のテスト

今回のテストでは、ログイン、indexページにアクセス、最初のページにユーザーがいることを確認、
         ページネーションのリンクがあることを確認
テスト用のデータベースに31人以上のユーザーがいる必要

fixtureにさらに30人のユーザーを追加する

test/fixtures/users.yml

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>

archer:
  name: Sterling Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>

lana:
  name: Lana Kane
  email: hands@example.gov
  password_digest: <%= User.digest('password') %>

malory:
  name: Malory Archer
  email: boss@example.gov
  password_digest: <%= User.digest('password') %>

<% 30.times do |n| %>
# さらに30人のユーザーを追加
user_<%= n %>:
  name:  <%= "User #{n}" %>
  email: <%= "user-#{n}@example.com" %>
  password_digest: <%= User.digest('password') %>
<% end %>
ubuntu:~/environment/sample_app (updating-users) $ rails generate integration_test users_index
Running via Spring preloader in process 7955
      invoke  test_unit
      create    test/integration/users_index_test.rb

ページネーションを含めたUsersIndexのテスト

test/integration/users_index_test.rb

require 'test_helper'

class UsersIndexTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end

  test "index including pagination" do
    log_in_as(@user)
    get users_path
    # テスト内のユーザーをログインさせる
    assert_template 'users/index'
    # URL(パス)が表示されているか?
    assert_select 'div.pagination'
    # assert_select HTMLの構造が適切かどうか、効率よく調べる
    # 多分paginationについて調べている
    User.paginate(page: 1).each do |user|
      assert_select 'a[href=?]', user_path(user), text: user.name
      # assert_select("セレクタ", 確認条件, ...)
    end
  end
end

テスト

ubuntu:~/environment/sample_app (updating-users) $ rails t
Running via Spring preloader in process 9397
Started with run options --seed 24343

  39/39: [============================] 100% Time: 00:00:05, Time: 00:00:05

Finished in 5.73793s
39 tests, 131 assertions, 0 failures, 0 errors, 0 skips

演習

1.
試しにリスト 10.45にあるページネーションのリンク(will_paginateの部分)を2つともコメントアウトしてみて、リスト 10.48のテストが red に変わるかどうか確かめてみましょう。

ubuntu:~/environment/sample_app (updating-users) $ rails t
Running via Spring preloader in process 10924
Started with run options --seed 17001

 FAIL["test_index_including_pagination", #<Minitest::Reporters::Suite:0x000056385676eed8 @name="UsersIndexTest">, 3.715851157999168]
 test_index_including_pagination#UsersIndexTest (3.72s)
        Expected at least 1 element matching "div.pagination", found 0..
        Expected 0 to be >= 1.
        test/integration/users_index_test.rb:15:in `block in <class:UsersIndexTest>'

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

Finished in 4.27249s
39 tests, 101 assertions, 1 failures, 0 errors, 0 skips

"div.pagination"が見つからない。

2.先ほどは2つともコメントアウトしましたが、1つだけコメントアウトした場合、テストが green のままであることを確認してみましょう。will_paginateのリンクが2つとも存在していることをテストしたい場合は、どのようなテストを追加すれば良いでしょうか? ヒント: 表 5.2を参考にして、数をカウントするテストを追加してみましょう。

ubuntu:~/environment/sample_app (updating-users) $ rails t
Running via Spring preloader in process 11099
Started with run options --seed 4981

  39/39: [============================] 100% Time: 00:00:02, Time: 00:00:02

Finished in 2.56118s
39 tests, 131 assertions, 0 failures, 0 errors, 0 skips

エラーメッセージにある通り1以上はあったほうが良いらしい。

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