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

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

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

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

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1324閲覧

RSpecのテストエラー

higmonta

総合スコア29

Ruby

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

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2022/08/15 20:04

RSpecのテストでエラーになってしまい、原因と解決方法が分からない為、質問させていただきます。

開発環境は、以下のようになります。
Rails 6.0.4.1
Ruby 2.5.1

コードは、以下のようになります。

[spec/system/users_spec.rb] require 'rails_helper' RSpec.describe "UsersSpec.rbs", type: :system do let(:user) { create(:user) } describe 'ログイン後' do before { login_as(user) } describe 'パスワードの変更機能の検証' do fit 'パスワードが変更できる' do visit profile_path click_on 'パスワードのリセット' fill_in 'email', with: user.email click_button '送信' expect(page).to have_content 'パスワードリセットのメールを送信しました' visit edit_password_reset_url(user.reload.reset_password_token) expect(page).to have_content user.email fill_in 'パスワード', with: 'update_password' fill_in 'パスワード確認', with: 'update_password' click_button '更新する' expect(page).to have_content('パスワードを変更しました') expect(current_path).to eq(login_path) end end

エラーは、以下のようになります。

higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % bundle exec rspec DEPRECATION WARNING: Initialization autoloaded the constants ActionText::ContentHelper and ActionText::TagHelper. Being able to do this is deprecated. Autoloading during initialization is going to be an error condition in future versions of Rails. Reloading does not reboot the application, and therefore code executed during initialization does not run again. So, if you reload ActionText::ContentHelper, for example, the expected changes won't be reflected in that stale Module object. These autoloaded constants have been unloaded. Please, check the "Autoloading and Reloading Constants" guide for solutions. (called from <top (required)> at /Users/higmonta/workspace/fishing_cooking/config/environment.rb:5) Run options: include {:focus=>true} UsersSpec.rbs ログイン後 パスワードの変更機能の検証 パスワードが変更できる (FAILED - 1) Failures: 1) UsersSpec.rbs ログイン後 パスワードの変更機能の検証 パスワードが変更できる Failure/Error: visit edit_password_reset_url(user.reload.reset_password_token) Net::ReadTimeout: Net::ReadTimeout [Screenshot]: /Users/higmonta/workspace/fishing_cooking/tmp/screenshots/failures_r_spec_example_groups_users_spec_rbs_nested_2_nested_3_パスワードが変更できる_512.png # ./spec/system/users_spec.rb:251:in `block (4 levels) in <top (required)>' Finished in 1 minute 19.1 seconds (files took 3.9 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/system/users_spec.rb:245 # UsersSpec.rbs ログイン後 パスワードの変更機能の検証 パスワードが変更できる

自分で試したことと考えたこと

タイムアウトとなっており、テスト実行時に時間がかかりすぎてしまいエラーになってしまうと考えました。
development環境では、ブラウザでは問題なくパスワードが変更できます。

テストコードをデバッグしたところ、以下のようになりました。

higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % bundle exec rspec DEPRECATION WARNING: Initialization autoloaded the constants ActionText::ContentHelper and ActionText::TagHelper. Being able to do this is deprecated. Autoloading during initialization is going to be an error condition in future versions of Rails. Reloading does not reboot the application, and therefore code executed during initialization does not run again. So, if you reload ActionText::ContentHelper, for example, the expected changes won't be reflected in that stale Module object. These autoloaded constants have been unloaded. Please, check the "Autoloading and Reloading Constants" guide for solutions. (called from <top (required)> at /Users/higmonta/workspace/fishing_cooking/config/environment.rb:5) Run options: include {:focus=>true} UsersSpec.rbs ログイン後 パスワードの変更機能の検証 ^A^BFrom:^A^B /Users/higmonta/workspace/fishing_cooking/spec/system/users_spec.rb:246 : 241: end 242: end 243: 244: describe 'パスワードの変更機能の検証' do 245: fit 'パスワードが変更できる' do => 246: binding.pry 247: visit profile_path 248: click_on 'パスワードのリセット' 249: fill_in 'email', with: user.email 250: click_button '送信' 251: expect(page).to have_content 'パスワードリセットのメールを送信しました' [1] pry(#<RSpec::ExampleGroups::UsersSpecRbs::Nested_2::Nested_3>)> visit profile_path => nil [2] pry(#<RSpec::ExampleGroups::UsersSpecRbs::Nested_2::Nested_3>)> click_on 'パスワードのリセット' => Obsolete #<Capybara::Node::Element> [3] pry(#<RSpec::ExampleGroups::UsersSpecRbs::Nested_2::Nested_3>)> fill_in 'email', with: user.email => #<Capybara::Node::Element tag="input" path="/HTML/BODY[1]/DIV[1]/DIV[1]/DIV[2]/DIV[1]/FORM[1]/DIV[1]/INPUT[1]"> [4] pry(#<RSpec::ExampleGroups::UsersSpecRbs::Nested_2::Nested_3>)> click_button '送信' => Obsolete #<Capybara::Node::Element> [5] pry(#<RSpec::ExampleGroups::UsersSpecRbs::Nested_2::Nested_3>)> expect(page).to have_content 'パスワードリセットのメールを送信しました' => true [6] pry(#<RSpec::ExampleGroups::UsersSpecRbs::Nested_2::Nested_3>)> user.reload.reset_password_token => "ckQnUfstgya-Rpk4pCfe" [7] pry(#<RSpec::ExampleGroups::UsersSpecRbs::Nested_2::Nested_3>)> visit edit_password_reset_url(user.reload.reset_password_token)Net::ReadTimeout: Net::ReadTimeout from /Users/higmonta/.rbenv/versions/2.5.1/lib/ruby/2.5.0/net/protocol.rb:181:in `rbuf_fill' [8] pry(#<RSpec::ExampleGroups::UsersSpecRbs::Nested_2::Nested_3>)>

visit edit_password_reset_url(user.reload.reset_password_token)の部分でタイムアウトになっており、アクセスができていない状態です。
プレフィックスも以下のようになっており、正しい状態です。

edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit

考えられるのがdevelopment環境の時に以下のようなURLだとポート番号が記載されてなく、アクセスできず、
http://localhost/password_resets/uqpk5GV1pjYCRv43fA4z/edit
以下のようにポート番号を記載することでアクセスできました。
http://localhost:3000/password_resets/uqpk5GV1pjYCRv43fA4z/edit

テスト環境で'visit edit_password_reset_url(user.reload.reset_password_token)'でアクセスする際にポート番号が記載されていないURLにアクセスしようとしているのではないかなと思いました。

以下の記載はしています。

[config/environments/test.rb] config.action_mailer.default_url_options = { host: "localhost", port: 3000 }

テスト実行時にタイムアウトになる理由と解消方法のご教示をお願いします。

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

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

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

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

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

guest

回答1

0

自己解決

visit edit_password_reset_url(user.reload.reset_password_token)をプレフィックスを使わずに以下のようにすることで、うまくいきました!!
visit "password_resets/#{user.reload.reset_password_token}/edit"

投稿2022/09/07 20:18

higmonta

総合スコア29

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問