LoginSignup
0
0

More than 1 year has passed since last update.

railsチュートリアル第13章 プロフィール画面のマイクロポストをテストする

Posted at

プロフィール画面のマイクロポストをテストする

プロフィール画面で表示されるマイクロポストに対して、統合テストを書いていきます。

ubuntu:~/environment/sample_app (user-microposts) $ rails generate integration_test users_profile
Running via Spring preloader in process 6991
      invoke  test_unit
      create    test/integration/users_profile_test.rb

生成されたマイクロポストfixture

test/fixtures/microposts.yml

orange:
  content: "I just ate an orange!"
  created_at: <%= 10.minutes.ago %>
  user: michael
  # ユーザーfixtureにある対応ユーザーに関連付けることをRailsに指示できます。

tau_manifesto:
  content: "Check out the @tauday site by @mhartl: https://tauday.com"
  created_at: <%= 3.years.ago %>
  user: michael

cat_video:
  content: "Sad cats are sad: https://youtu.be/PKffm2uI4dk"
  created_at: <%= 2.hours.ago %>
  user: michael

most_recent:
  content: "Writing a short test"
  created_at: <%= Time.zone.now %>
  user: michael

<% 30.times do |n| %>
micropost_<%= n %>:
  content: <%= Faker::Lorem.sentence(word_count: 5) %>
  created_at: <%= 42.days.ago %>
  user: michael
<% end %>

Userプロフィール画面に対するテスト

test/integration/users_profile_test.rb

require 'test_helper'

class UsersProfileTest < ActionDispatch::IntegrationTest
  include ApplicationHelper

  def setup
    @user = users(:michael)
    # テストユーザーを設定
  end

  test "profile display" do
    get user_path(@user)
    # ユーザーページに行くを要求
    assert_template 'users/show'
    # showアクションを表示されるか?
    # ユーザーページが表示されているか?
    assert_select 'title', full_title(@user.name)
    # ユーザーページのタイトルタグにユーザー名が表示されているか?
    assert_select 'h1', text: @user.name
    # h1タグにユーザー名が表示されているか?
    assert_select 'h1>img.gravatar'
    # h1タグ(トップレベルの見出し)の内側にある、gravatarクラス付きのimgタグがあるかどうかをチェックできます。
    assert_match @user.microposts.count.to_s, response.body
    # response.bodyにはそのページの完全なHTMLが含まれています
    # そのページのどこかしらにマイクロポストの投稿数が存在するのであれば、
    #   次のように探し出してマッチできる
    assert_select 'div.pagination'
    # ページネーションが表示されているか?
    @user.microposts.paginate(page: 1).each do |micropost|
      assert_match micropost.content, response.body
      # マイクロポストがあるかどうか?
    end
  end
end
テスト
ubuntu:~/environment/sample_app (user-microposts) $ rails t
Running via Spring preloader in process 8880
Started with run options --seed 3883

  54/54: [============================] 100% Time: 00:00:09, Time: 00:00:09

Finished in 9.41028s
54 tests, 288 assertions, 0 failures, 0 errors, 0 skips

演習

1.
リスト 13.28にある2つの'h1'のテストが正しいか確かめるため、該当するアプリケーション側のコードをコメントアウトしてみましょう。テストが green から red に変わることを確認してみてください。

ubuntu:~/environment/sample_app (user-microposts) $ rails t
Running via Spring preloader in process 9267
Started with run options --seed 35939

 FAIL["test_profile_display", #<Minitest::Reporters::Suite:0x000055f1ec686990 @name="UsersProfileTest">, 1.963515799000561]
 test_profile_display#UsersProfileTest (1.96s)
        <Michael Example> expected but was
        <>..
        Expected 0 to be >= 1.
        test/integration/users_profile_test.rb:19:in `block in <class:UsersProfileTest>'

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

Finished in 2.55123s
54 tests, 224 assertions, 1 failures, 0 errors, 0 skips

2.
リスト 13.28にあるテストを変更して、will_paginateが1度のみ表示されていることをテストしてみましょう。ヒント: 表 5.2を参考にしてください。

 assert_select 'h1>img.gravatar'
    # h1タグ(トップレベルの見出し)の内側にある、
    #   gravatarクラス付きのimgタグがあるかどうかをチェックできます。
    assert_match @user.microposts.count.to_s, response.body
    # response.bodyにはそのページの完全なHTMLが含まれています
    # そのページのどこかしらにマイクロポストの投稿数が存在するのであれば、
    #   次のように探し出してマッチできる
    assert_select 'div.pagination'
    # ページネーションが表示されているか?
    assert_select 'div.pagination', count:1
    @user.microposts.paginate(page: 1).each do |micropost|
      assert_match micropost.content, response.body
      # マイクロポストがあるかどうか?
    end
  end
end

<%= will_paginate renderer: BootstrapPagination::Rails %>
とすると
ページネーションのスタイルが変わるらしい。

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