【81日目】【1日20分のRailsチュートリアル】【第8章】レイアウト変更確認用テストを追加する
今日は「8.2.4 レイアウトの変更をテストする」のテストを作成するところから。
8.2.4 レイアウトの変更をテストする
ハッシュ化されていない生のパスワードも参照できると便利です。
(中略)
この状況を切り抜けるために、テスト用のフィクスチャユーザーでは全員同じパスワード「password」を使用することにします。これはフィクスチャでよく使われる手法です。
生のパスワードはデータベースに保存できない(カラムがない)し、
ハッシュ値からパスワード復元もできないのでテストユーザーには「password」で統一する、てことかな。
準備はできたのでテストを追加。
test/integration/users_login_test.rb
class UsersLoginTest < ActionDispatch::IntegrationTest def setup @user = users(:michael) end : test "login with valid information" do get login_path post login_path, session: { email: @user.email, password: 'password' } assert_redirected_to @user follow_redirect! assert_template 'users/show' assert_select "a[href=?]", login_path, count: 0 assert_select "a[href=?]", logout_path assert_select "a[href=?]", user_path(@user) end end
setup
メソッドと今回のテストを追加。
ほぼ昨日の記事で書いた手順通りかな。
assert_template 'users/show'
は今表示されているページのテンプレートが想定通りなのを確認してるっぽい。
テスト実行して問題ないことを確認。
$ bundle exec rake test TEST=test/integration/users_login_test.rb \ > TESTOPTS="--name test_login_with_valid_information" 1 tests, 6 assertions, 0 failures, 0 errors, 0 skips
ちょっと早いけれど今日はここまで。
今日の作業時間は【14分】。
次は「8.2.5 ユーザー登録時にログイン」から。