Docker上でRailsアプリのRSpecテスト実行を試みています。
新規ユーザー登録画面のsystem specで"Capybara::ElementNotFound: Unable to find field ..."が長時間解決できません。
ご助言をいただけると幸いです。
開発環境
- ruby 3.0.1
- Rails 6.1.4.4
- puma 5.5.2
- nginx 1.21.6
- docker 20.10.12
- docker-compos 1.29.2
前提事項
ユーザー認証にはdeviseを使用しています。
# ディレクトリ構造 myapp ├── app │ ├── assets │ ├── ... │ └── views │ ├── ... │ ├── ... │ └── users │ ├── ... │ ├── registrations │ │ ├── edit.html.erb │ │ └── new.html.erb <= ここでエラー発生 │ ├── sessions │ │ └── new.html.erb │ ├── sessions │ ├── ... │ ├── index.html.erb │ └── show.html.erb ├── ... ├── Dockerfile ├── tailwind.config.js └── ...
起きている問題
docker-compose exec app bash
でコンテナ内に入りbundle exec rspec
を実行すると、新規登録フォームの「アカウント名 *」のみ表題のエラーが出る。
bash
1$ bundle exec rspec 2 3Running via Spring preloader in process 841 4 5User 6 User CRUD 7 ログイン前 8 ユーザー新規登録 9 フォームの入力値が正常 10 ユーザーの新規作成が成功 (FAILED - 1) 11 メールアドレス未記入 12 ユーザーの新規作成が失敗 (FAILED - 2) 13 登録済メールアドレス 14 ユーザーの新規作成が失敗 (FAILED - 3) 15 パスワードが未記入 16 ユーザーの新規作成が失敗 (FAILED - 4) 17 パスワードが6文字未満かつパスワード(確認)の入力値が異常 18 ユーザーの新規作成が失敗 (FAILED - 5) 19 20Failures: 21 22 1) User User CRUD ログイン前 ユーザー新規登録 フォームの入力値が正常 ユーザーの新規作成が成功 23 Failure/Error: fill_in 'アカウント名 *', with: "test" 24 25 Capybara::ElementNotFound: 26 Unable to find field "アカウント名 *" that is not disabled 27 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/node/finders.rb:303:in `block in synced_resolve' 28 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/node/base.rb:83:in `synchronize' 29 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/node/finders.rb:292:in `synced_resolve' 30 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/node/finders.rb:53:in `find' 31 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/node/actions.rb:91:in `fill_in' 32 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/session.rb:772:in `fill_in' 33 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/dsl.rb:53:in `call' 34 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/dsl.rb:53:in `fill_in' 35 # ./spec/systems/users_spec.rb:13:in `block (6 levels) in <main>' 36 37 2) User User CRUD ログイン前 ユーザー新規登録 メールアドレス未記入 ユーザーの新規作成が失敗 38 Failure/Error: fill_in 'アカウント名 *', with: 'test' 39 40 Capybara::ElementNotFound: 41 Unable to find field "アカウント名 *" that is not disabled 42...
関連するコード
html.erb
1# myapp/views/users/registrations/new.html.erb 2... 3<div class="bg-white py-6 sm:py-8 lg:py-12"> 4 <div class="max-w-screen-2xl px-4 md:px-8 mx-auto justify-center"> 5 <h2 class="text-gray-800 text-2xl lg:text-3xl font-bold text-center mb-4 md:mb-8">アカウント登録</h2> 6 <%= form_with(model: @user, url: registration_path(@user)) do |f| %> 7 <div class="flex justify-center items-center p-4"> 8 <%= render "devise/shared/error_messages", resource: resource %> 9 </div> 10 <div class="max-w-lg border border-gray-300 shadow rounded-lg mx-auto"> 11 <div class="flex flex-col gap-4 p-4 md:p-8"> 12 <div> 13 <%= f.label :name, "アカウント名 *", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 14 <%= f.text_field :name, autofocus: true, autocomplete: "name", class: 'w-full bg-gray-50 text-gray-800 border border-gray-300 focus:ring ring-indigo-500 rounded outline-none transition duration-100 px-3 py-2' %> 15 </div> 16 <div> 17 <%= f.label :email, "メールアドレス *", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 18 <%= f.text_field :email, autofocus: true, autocomplete: "email", class: 'w-full bg-gray-50 text-gray-800 border border-gray-300 focus:ring ring-indigo-500 rounded outline-none transition duration-100 px-3 py-2' %> 19 </div> 20...
試したこと
よくあるエラーの様で、対象となるfieldのname属性の値を調べてfill inの要素にするなどの方法を試しましたが結果は変わりませんでした。
https://qiita.com/pooooon/items/4fbc429d07e4b65ed928
https://stackoverflow.com/questions/70671277/capybara-with-rails-6-1-4-4-element-not-found
html
1<!-- Chromeブラウザ --> 2 3<div> 4 <label class="inline-block text-gray-800 text-sm sm:text-base mb-2" for="user_name">アカウント名 *</label> 5 <input autofocus="autofocus" autocomplete="name" class="w-full bg-gray-50 text-gray-800 border border- 6 gray-300 focus:ring ring-indigo-500 rounded outline-none transition duration-100 px-3 py-2" type="text" 7 value="" name="user[name]" id="user_name"> 8</div> 9<div> 10 <label class="inline-block text-gray-800 text-sm sm:text-base mb-2" for="user_email">メールアドレス *</label> 11 <input autofocus="autofocus" autocomplete="email" class="w-full bg-gray-50 text-gray-800 border border- 12 gray-300 focus:ring ring-indigo-500 rounded outline-none transition duration-100 px-3 py-2" type="text" 13 value="" name="user[email]" id="user_email"> 14</div> 15...
ruby
1# myapp/spec/systems/users_spec.rb 2 3require 'rails_helper' 4 5RSpec.describe User, type: :system do 6 describe 'User CRUD' do 7 describe 'ログイン前' do 8 let(:user) { create(:user) } 9 let(:other_user) { create(:user) } 10 11 describe 'ユーザー新規登録' do 12 context 'フォームの入力値が正常' do 13 it 'ユーザーの新規作成が成功' do 14 visit new_user_registration_path 15 fill_in "user[name]", with: "test" 16 fill_in 'メールアドレス *', with: 'test@example.com' 17 fill_in 'パスワード', with: 'password' 18 fill_in 'パスワード(確認) *', with: 'password' 19 click_button '認証メールを送信する' 20 expect(current_path).to eq new_user_session_path 21 expect(page).to have_content '本人確認用のメールを送信しました。メール内のリンクからアカウントを有効化させてください。' 22 end 23 end 24 context 'メールアドレス未記入' do 25 it 'ユーザーの新規作成が失敗' do 26 visit new_user_registration_path 27 fill_in "user[name]", with: 'test' 28 fill_in 'メールアドレス *', with: nil 29 fill_in 'パスワード', with: 'password' 30 fill_in 'パスワード(確認) *', with: 'password' 31 click_button '認証メールを送信する' 32 expect(current_path).to eq users_path 33 expect(page).to have_content 'Eメールを入力してください' 34 end 35 end 36...
※"user_name"と"user[name]"の両方を試しました。
解決につながるヒントをいただけないでしょうか。
よろしくお願いいたします。
あなたの回答
tips
プレビュー