1
1

Ruby on Rails チュートリアル 第11章演習解答(第7版)

Posted at

概要

11.3.3の演習問題が難しく、ネット上に解答が見つからなかったので忘備録として記載してます。
rails tでGREENでしたが間違っていたらすみません。
ご指摘いただけると大変助かります。

解答

リスト 11.42

test/integration/users_index_test.rb
.
.
.
  test "should display only activated users" do
    # ページにいる最初のユーザーを無効化する。
    # 無効なユーザーを作成するだけでは、
    # Railsで最初のページに表示される保証がないので不十分
    User.paginate(page: 1).first.toggle!(:activated)
    # /usersを再度取得して、無効化済みのユーザーが表示されていないことを確かめる
    get users_path
    # 表示されているすべてのユーザーが有効化済みであることを確かめる
    assigns(:users).each do |user|
      assert user.activated?
    end
  end
end
.
.
.

リスト 11.43

テンプレート通りのため省略

リスト 11.44

test/integration/user_show_test.rb
require "test_helper"

class UserShowTest < ActionDispatch::IntegrationTest

  def setup
    @inactive_user  = users(:inactive)
    @activated_user = users(:archer)
  end

  test "should redirect when user not activated" do
    get user_path(@inactive_user)
    assert_response :redirect
    assert_redirected_to root_url
  end

  test "should display user when activated" do
    get user_path(@activated_user)
    assert_response :success
    assert_template 'users/show'
  end
end
1
1
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
1
1