質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Q&A

解決済

1回答

2616閲覧

systemspec実行時、Capybara::ElementNotFoundのエラーを解決したい

hinata_hakumai

総合スコア3

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

0グッド

0クリップ

投稿2021/11/17 06:48

前提・実現したいこと

投稿アプリを作成しています。
capybaraを導入し、systemspecでテストを行いたいのですがどうやってもエラーが
解決しないため,ご助言いただければと思います。

試したこと

下記のように、click_linkを用いたテストを行ったのですがリンクが見つからないとのエラー文が出ます。さらに、fill_inを用いたログインのテストも通らないです。

fill_inに関して試したこと
①"user[email]"を"user_email"や"メールアドレス"に変更。
②pathが違うのかと思い、visit "/users/sign_in"に変更したり、routesを確認。③capybara導入の見直し
④dockerコンテナの再起動
試行錯誤を繰り返したものの、いずれも変化がなく、手詰まりです。

どこか見直した方がいい箇所やアドバイスがあればご教授いただけますと幸いです。
よろしくお願い致します。

発生している問題・エラーメッセージ

Failure/Error: click_link "サインインする" Capybara::ElementNotFound: Unable to find link "サインインする"
Failure/Error: fill_in "user[email]", with: "test1@example.com" Capybara::ElementNotFound: Unable to find field "user[email]" that is not disabled

該当のソースコード

ruby

1require 'rails_helper' 2 3RSpec.describe 'ログイン', type: :system do 4 describe 'ログイン' do 5 before do 6 visit new_user_registration_path 7 end 8 9 it '「サインインする」をクリックすると正しく遷移する' do 10 click_link "サインインする" 11 expect(current_path).to eq new_user_session_path 12 end 13 14        it 'ログインできる' do 15 visit root_path 16 fill_in "user[email]", with: "test1@example.com" 17 fill_in "user[password]", with: "foobar" 18 click_button "ログインする" 19 end 20 end 21end

ruby

1<% provide(:title, "ユーザー登録") %> 2<div class="main"> 3 <div class="card devise-card"> 4 <div class="form-wrap"> 5 <div class="form-group text-center"> 6 <h2 class="logo-img mx-auto"></h2> 7 <p class="text-secondary">ユーザー登録</p> 8 </div> 9 <%= form_with scope: resource, as: resource_name, url: registration_path(resource_name), local: true do |f| %> 10 <%= render 'devise/shared/error_messages' %> 11 <div class="form-group"> 12 <%= f.email_field :email, autofocus: true, placeholder: "メールアドレス", class: "form-control" %> 13 </div> 14 <div class="form-group"> 15 <%= f.text_field :name, autofocus: true, placeholder: "名前", class: "form-control" %> 16 </div> 17 <div class="form-group"> 18 <%= f.label :profile_photo, "プロフィール画像" %> 19 <%= f.file_field :profile_photo, class: "profile-control" %> 20 </div> 21 <div class="form-group"> 22 <%= f.password_field :password, autocomplete: "off", placeholder: "パスワード", class: "form-control" %> 23 </div> 24 <div class="form-group"> 25 <%= f.password_field :password_confirmation, autocomplete: "off", placeholder: "パスワードの確認", class: "form-control" %> 26 </div> 27 <div class="actions mt-3"> 28 <%= f.submit "登録する", class: "btn btn-primary w-100" %> 29 </div> 30 <% end %> 31 <br> 32 <p class="devise-link"> 33 アカウントをお持ちですか? 34 <%= link_to "サインインする", new_user_session_path %> 35 </p> 36 </div> 37 </div> 38</div> 39

ruby

1<% provide(:title, "ログイン") %> 2<div class="main"> 3 <div class="card devise-card"> 4 <div class="form-wrap"> 5 <div class="form-group text-center"> 6 <h2 class="logo-img mx-auto">ログイン</h2> 7 </div> 8 <%= form_with scope: resource, as: resource_name, url: session_path(resource_name), local: true do |f| %> 9 <%= devise_error_messages! %> 10 <div class="form-group"> 11 <label for="user_email">テストユーザーの方は「example@example.com」をご利用ください。</label> 12 <%= f.email_field :email, autofocus: true, placeholder: "メールアドレス", class: "form-control", id: "user_email" %> 13 </div> 14 <div class="form-group"> 15 <label for="user_password">テストユーザーの方は「foobar」をご利用ください。</label> 16 <%= f.password_field :password, autocomplete: "off", placeholder: "パスワード", class: "form-control", id: "user_password" %> 17 </div> 18 <div class="actions mt-3"> 19 <%= f.submit "ログインする", class: "btn btn-primary w-100" %> 20 </div> 21 <% end %> 22 <br> 23 <p class="devise-link"> 24 アカウントをお持ちでないですか? 25 <%= link_to "登録する", new_user_registration_path %> 26 </p> 27 </div> 28 </div> 29</div>

ruby

1rails_helper.rb 2 3require 'spec_helper' 4ENV['RAILS_ENV'] ||= 'test' 5require File.expand_path('../config/environment', __dir__) 6if Rails.env.production? 7 abort('The Rails environment is running in production mode!') 8end 9require 'rspec/rails' 10require 'capybara/rspec' 11require 'capybara/rails' 12 13Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f } 14 15begin 16 ActiveRecord::Migration.maintain_test_schema! 17rescue ActiveRecord::PendingMigrationError => e 18 puts e.to_s.strip 19 exit 1 20end 21RSpec.configure do |config| 22 config.fixture_path = "#{::Rails.root}/spec/fixtures" 23 24 config.use_transactional_fixtures = true 25 26 config.infer_spec_type_from_file_location! 27 28 config.include Devise::Test::ControllerHelpers, type: :controller 29 config.include RequestSpecHelper, type: :request 30 config.include Devise::Test::IntegrationHelpers, type: :system 31 config.include FactoryBot::Syntax::Methods 32end

イメージ説明

補足情報(FW/ツールのバージョンなど)

MacOS
docker

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

configにconfig.hosts << "www.example.com"を記入することで解決しました。

投稿2021/11/17 12:27

hinata_hakumai

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問