6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

システムスペックではvisit hoge_urlではなくvisit hoge_pathと書かないとNet::ReadTimeoutエラーが発生する

Posted at

発生した問題

Hello worldレベルのごく簡単なRailsアプリを作成し、rspec-railsをインストールして次のようなシステムスペックを書きました。

require 'rails_helper'

RSpec.describe "Home", type: :system do
  before do
    driven_by(:selenium_chrome_headless)
  end

  example do
    visit root_url
    expect(page).to have_content 'Home#index'
  end
end

テストを実行すると・・・あれ?

$ bundle exec rspec
DEBUGGER: Attaching after process 28722 fork to child process 28745

(何も起きない)

この状態が何十秒も続き、最終的には以下のエラーが出て終了しました。

$ bundle exec rspec
DEBUGGER: Attaching after process 28722 fork to child process 28745
F

Failures:

  1) Homes 
     Failure/Error: visit root_url
     
     Net::ReadTimeout:
       Net::ReadTimeout with #<Socket:(closed)>
     
     [Screenshot Image]: /Users/jnito/dev/sandbox/without-active-record/tmp/capybara/failures_r_spec_example_groups_homes_example_at___spec_system_homes_spec_rb_8_381.png

     
     # ./spec/system/homes_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 1 minute 16.35 seconds (files took 0.5839 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/system/homes_spec.rb:8 # Homes 

ちなみにドライバを:rack_testに変えるとテストはパスします。

   before do
-    driven_by(:selenium_chrome_headless)
+    driven_by(:rack_test)
   end
$ bundle exec rspec
.

Finished in 0.06872 seconds (files took 0.63488 seconds to load)
1 example, 0 failures

はて????

解決方法

visit root_urlvisit root_pathに変えると直りました。たったこれだけ!!

   example do
-    visit root_url
+    visit root_path
     expect(page).to have_content 'Home#index'
   end
$ bundle exec rspec
DEBUGGER: Attaching after process 28608 fork to child process 28634
.

Finished in 1.44 seconds (files took 0.60104 seconds to load)
1 example, 0 failures

ちなみにデフォルトの状態ではroot_urlroot_pathはそれぞれ以下のような値になっていました。

puts root_url  #=> http://www.example.com/
puts root_path #=> /

これ、以前もハマった記憶があるんですが、すっかり忘れてまたハマってしまいました……😓
みなさんもお気を付けください。

確認したバージョン

  • ruby 3.1.2
  • rails 7.0.4
  • rspec-rails 6.0.0.rc1
  • capybara 3.37.1
  • selenium-webdriver 4.4.0
  • Chrome 105.0.5195.125
6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?