LoginSignup
0
0

More than 3 years have passed since last update.

ユーザーを検索する機能を実装するの巻 その2

Posted at

この記事について

前回の記事
https://qiita.com/Shinsama12123/items/fca4427a3611b5711bb0
ユーザーを検索する機能を実装することができたので、次にテストの記事をあげるみたいなことを書いていました。
早速...。

テスト

spec/integration/user_index_spec.rb
require 'rails_helper'

RSpec.describe "Search",type: :integration do

  #ユーザー検索
  it("users search") do
    log_in_as(@admin)
    #全てのユーザー
    get(users_path, :params => ({ :q => ({ :name_cont => "" }) }))
    User.paginate(:page => 1).each do |user|
    assert_select("a[href=?]", user_path(user), :text => user.name)
    end
    assert_select("title", "ユーザー検索🐼パパっ子")

    #ユーザーの検索結果
    get(user_path, :params => ({ :q => ({ :name_cont => "a" }) }))
    q = User.ransack(:name_cont => "a", :activated_true => true)
    q.result.paginate(:page => 1).each do |user|
    assert_select("a[href=?]", user_path(user), :text => user.name)
    end
    assert_select("title", "検索結果🐼パパっ子")

    #見つからなかった場合
    get(users_path, :params => ({ :q => ({ :name_cont => "abcdefghijk" }) }))
    expect(response.body).to(match("ユーザーは見つかりませんでした。"))

    #'全てのユザー'に戻っていることの確認
    get(users_path, :params => ({ :q => ({ :name_cont => "" }) }))
    assert_select("title", "ユーザー検索🐼パパっ子")
  end
end

テストについて

1.全ユーザーの表示 
2.検索結果(あり)
3.検索結果(なし)

1、2についてはタイトルのチェック、3についてはメッセージ確認を行うことにします。
テストが通ったことを確認して終了です。

実は、私はテストはまだ通ってはいません。log_in_as(@admin)の部分にエラーがあるようで、なので実際にテストが通ったら、また編集します。すみません。

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