【Ruby】結合テスト-フォームに値を入力してログインするテスト
前回からハマっていたエラー沼から無事脱出したため備忘録としてこの記事を書いています。 解決できると、そういうことだったんだフーンみたいな拍子抜け感はいつもの感じ。 でもこの件に関しては今後忘れてはいけないことなのでしっかりとアウトプットしていきます。
・問題点・ ①誕生日の項目にエラーが発生してしまい、登録できない。
②すべての項目を入力している(バリデーションもクリアしている)のにUserモデルのカウントが上がらない
・目標着地点・ ①誕生日の値を登録すること ②データベースに入力した値を保存することでUserモデルのカウントを1上げ、トップページに戻ること
・目標着地点・ ①誕生日項目に値を入力 今回、誕生日入力フォームはプルダウンで選ぶ方式のため、テストコード内でfill_inメソッドは使えません。 なので以下のように記述しました。
it 'ヘッダーの新規登録ボタンを押し、各項目を入力するとユーザーの新規登録ができること' do visit new_user_registration_path expect(current_path).to eq(new_user_registration_path) fill_in 'nickname', with: @user.name fill_in 'email', with: @user.email fill_in 'password', with: @user.password fill_in 'password-confirmation', with: @user.password_confirmation fill_in 'last-name', with: @user.last_name fill_in 'first-name', with: @user.first_name fill_in 'last-name-kana', with: @user.last_name_kana fill_in 'first-name-kana', with: @user.first_name_kana select '1930',from: "user[birth_date(1i)]" select '10',from: "user[birth_date(2i)]" select '30',from: "user[birth_date(3i)]"
select '値', from: "name属性"
selectについては、コチラを参考させていただいております。 from以下に記載するname属性はどこじゃ!?と該当のビューファイル内を懸命に探しても見つからず途方にくれていたのですが、こちらも上記参考記事にしっかり書いてありました。ちゃんと読まないとダメですね。 ちゃんと検証ツール見たらしっかりname属性、表示されていました。
ちなみに、先日の記事で誕生日のダミーを生成する方法を記述しました。 ダミー値とselectで指定した値が違うとだめなのでは…と思ったのですが、そこは関係なかったようです。
- FactoryBot生成の際には誕生日の値もちゃんと生成する (すべての値を生成しないと初っ端からエラーに…)
- selectメソッドで値を入力する際はダミーと違ってOK
以上が誕生日の値入力(というかプルダウンで値を入力するタイプ)の注意点です。
②すべての項目が入力されているのにモデルカウントが上がらない
RSpec.describe "新規登録・ログインについて", type: :system do before do @user = FactoryBot.create(:user) end it 'ヘッダーの新規登録ボタンを押し、各項目を入力するとユーザーの新規登録ができること' do visit new_user_registration_path expect(current_path).to eq(new_user_registration_path) fill_in 'nickname', with: @user.name fill_in 'email', with: @user.email fill_in 'password', with: @user.password fill_in 'password-confirmation', with: @user.password_confirmation fill_in 'last-name', with: @user.last_name fill_in 'first-name', with: @user.first_name fill_in 'last-name-kana', with: @user.last_name_kana fill_in 'first-name-kana', with: @user.first_name_kana select '1930',from: "user[birth_date(1i)]" select '10',from: "user[birth_date(2i)]" select '30',from: "user[birth_date(3i)]" expect{ find('input[name="commit"]').click }.to change{User.count}.by(1) expect(current_path).to eq(root_path) end ~後略~
ここでの注意点は、値生成時に使用するメソッド。
ここを誤っていたため、エラーが発生していました。
・build→値の生成のみ
・create→値の保存まで
before do
で値を保存までしていたら、クリックしても保存できないですよね…(というかメールアドレスを重複して登録できないのでエラーになる)
なので以下追記しました。
RSpec.describe "新規登録・ログインについて", type: :system do before do @user = FactoryBot.create(:user) end it 'ヘッダーの新規登録ボタンを押し、各項目を入力するとユーザーの新規登録ができること' do @user = FactoryBot.build(:user) #追記 visit new_user_registration_path expect(current_path).to eq(new_user_registration_path) fill_in 'nickname', with: @user.name fill_in 'email', with: @user.email fill_in 'password', with: @user.password fill_in 'password-confirmation', with: @user.password_confirmation fill_in 'last-name', with: @user.last_name fill_in 'first-name', with: @user.first_name fill_in 'last-name-kana', with: @user.last_name_kana fill_in 'first-name-kana', with: @user.first_name_kana select '1930',from: "user[birth_date(1i)]" select '10',from: "user[birth_date(2i)]" select '30',from: "user[birth_date(3i)]" expect{ find('input[name="commit"]').click }.to change{User.count}.by(1) expect(current_path).to eq(root_path) end ~後略~
以上で目標着地点到達です。
今回、以下の記事を参考にいたしました。ありがとうございました。 【結合テストコード】date_selectから要素を選択する方法