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

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

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

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

Ruby

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

Chrome

Google Chromeは携帯、テレビ、デスクトップなどの様々なプラットフォームで利用できるウェブブラウザです。Googleが開発したもので、Blink (レンダリングエンジン) とアプリケーションフレームワークを使用しています。

RSpec

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

Ruby on Rails

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

Q&A

解決済

1回答

7951閲覧

『Ruby on Rails 5 速習実践ガイド』のRSpecの実行部分でテストが通らなく詰まっています。

Kazuki_Sasaki

総合スコア10

Ruby on Rails 5

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

Ruby

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

Chrome

Google Chromeは携帯、テレビ、デスクトップなどの様々なプラットフォームで利用できるウェブブラウザです。Googleが開発したもので、Blink (レンダリングエンジン) とアプリケーションフレームワークを使用しています。

RSpec

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

Ruby on Rails

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

0グッド

3クリップ

投稿2019/05/02 05:42

編集2019/05/02 05:54

前提・実現したいこと

RSpecのテストを通るようにしたいです。

環境
-OS: Ubuntu 18.04.1 LTS (WSL windows10
-Rails 5.2.3

Ubuntu環境にChromeが必要そうだったのでChromeとChromeDriverをインストールしました。

usr@desktop@@@@:~/taskleaf$ google-chrome -version => Google Chrome 74.0.3729.131 usr@desktop@@@@:~/taskleaf$ google-chrome => [337:337:0502/140113.683623:ERROR:browser_dm_token_storage_linux.cc(101)] Error: /etc/machine-id contains 0 characters (32 were expected). (google-chrome:488): Gtk-WARNING **: 14:06:56.681: cannot open display: usr@desktop@@@@:~/taskleaf$ chromedriver -v => ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}) usr@desktop@@@@:~/taskleaf$ chromedrivers => Starting ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}) on port 9515 Only local connections are allowed. Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.

gemlist

1..省略 2 3webdrivers (3.8.0) 4webrick (default: 1.4.2) 5websocket-driver (0.7.0) 6websocket-extensions (0.1.3) 7xmlrpc (0.3.0) 8xpath (3.2.0) 9zlib (default: 1.0.0)

現場で使える Ruby on Rails 5速習実践ガイドの205ページで止まっている状況です。

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

一番最初に発生したエラー

Failures: 1) タスク管理機能 一覧表示機能 ユーザーAがログインしているとき ユーザーAが作成したタスクが表示される Got 0 failures and 2 other errors: 1.1) Failure/Error: visit login_path Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515 # ./spec/system/tasks_spec.rb:14:in `block (4 levels) in <top (required)>' 1.2) Failure/Error: raise Error::WebDriverError, cannot_connect_error_text Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515

次に発生したエラー
https://qiita.com/jnchito/items/f9c3be449fd164176efa
chromedriver-helperというgemのサポートが終了したのでwebdriversというgemに移行することが推奨されているということなので

gem uninstall chromedriver-helper
Gemfileにwebdriversを追記
Gemfileのchromedriver-helperを削除
bundle install

の手順を実行。

Failures: 1) タスク管理機能 一覧表示機能 ユーザーAがログインしているとき ユーザーAが作成したタスクが表示される Got 0 failures and 2 other errors: 1.1) Failure/Error: visit login_path NoMethodError: undefined method `strip' for nil:NilClass # ./spec/system/tasks_spec.rb:14:in `block (4 levels) in <top (required)>' 1.2) Failure/Error: `#{executable} --product-version`.strip NoMethodError: undefined method `strip' for nil:NilClass

現在発生しているエラー
そもそもChromeが入っていないからエラーになるんじゃないか?
と感じて、ubuntuにChromeをインストールしました。よくわからないのでChromeDriverもインストールしました。

bundle exec rspec spec/system/tasks_spec.rb Capybara starting Puma... * Version 3.12.1 , codename: Llamas in Pajamas * Min threads: 0, max threads: 4 * Listening on tcp://127.0.0.1:51578 F Failures: 1) タスク管理機能 一覧表示機能 ユーザーAがログインしているとき ユーザーAが作成したタスクが表示される Got 0 failures and 2 other errors: 1.1) Failure/Error: visit login_path Net::ReadTimeout: Net::ReadTimeout # ./spec/system/tasks_spec.rb:15:in `block (4 levels) in <top (required)>' 1.2) Failure/Error: @io.to_io.wait_readable(@read_timeout) or raise Net::ReadTimeout Net::ReadTimeout: Net::ReadTimeout | |省略 | Finished in 2 minutes 14.3 seconds (files took 3.07 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/system/tasks_spec.rb:21 # タスク管理機能 一覧表示機能 ユーザーAがログインしているとき ユーザーAが作成したタスクが表示される /home/user/.rbenv/versions/2.5.1/lib/ruby/2.5.0/net/http.rb:939:in `rescue in block in connect': Failed to open TCP connection to 127.0.0.1:9515 (Connection refused - connect(2) for "127.0.0.1" port 9515) (Errno::ECONNREFUSED)

該当のソースコード

spec/rails_helper.rb

ruby

1require 'spec_helper' 2ENV['RAILS_ENV'] ||= 'test' 3require File.expand_path('../../config/environment', __FILE__) 4# Prevent database truncation if the environment is production 5abort("The Rails environment is running in production mode!") if Rails.env.production? 6require 'rspec/rails' 7# Add additional requires below this line. Rails is not loaded until this point! 8 9# Requires supporting ruby files with custom matchers and macros, etc, in 10# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are 11# run as spec files by default. This means that files in spec/support that end 12# in _spec.rb will both be required and run as specs, causing the specs to be 13# run twice. It is recommended that you do not name files matching this glob to 14# end with _spec.rb. You can configure this pattern with the --pattern 15# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. 16# 17# The following line is provided for convenience purposes. It has the downside 18# of increasing the boot-up time by auto-requiring all files in the support 19# directory. Alternatively, in the individual `*_spec.rb` files, manually 20# require only the support files necessary. 21# 22# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f } 23 24# Checks for pending migrations and applies them before tests are run. 25# If you are not using ActiveRecord, you can remove these lines. 26begin 27 ActiveRecord::Migration.maintain_test_schema! 28rescue ActiveRecord::PendingMigrationError => e 29 puts e.to_s.strip 30 exit 1 31end 32RSpec.configure do |config| 33 34 # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 35 config.fixture_path = "#{::Rails.root}/spec/fixtures" 36 37 # If you're not using ActiveRecord, or you'd prefer not to run each of your 38 # examples within a transaction, remove the following line or assign false 39 # instead of true. 40 config.use_transactional_fixtures = true 41 42 # RSpec Rails can automatically mix in different behaviours to your tests 43 # based on their file location, for example enabling you to call `get` and 44 # `post` in specs under `spec/controllers`. 45 # 46 # You can disable this behaviour by removing the line below, and instead 47 # explicitly tag your specs with their type, e.g.: 48 # 49 # RSpec.describe UsersController, :type => :controller do 50 # # ... 51 # end 52 # 53 # The different available types are documented in the features, such as in 54 # https://relishapp.com/rspec/rspec-rails/docs 55 config.infer_spec_type_from_file_location! 56 57 # Filter lines from Rails gems in backtraces. 58 config.filter_rails_from_backtrace! 59 # arbitrary gems may also be filtered via: 60 # config.filter_gems_from_backtrace("gem name") 61end

spec/spec_helper.rb

ruby

1require 'capybara/rspec' 2 3RSpec.configure do |config| 4 5 config.before(:each, type: :system) do 6 driven_by :selenium_chrome_headless 7 end 8 # rspec-expectations config goes here. You can use an alternate 9 # assertion/expectation library such as wrong or the stdlib/minitest 10 # assertions if you prefer. 11 config.expect_with :rspec do |expectations| 12 # This option will default to `true` in RSpec 4. It makes the `description` 13 # and `failure_message` of custom matchers include text for helper methods 14 # defined using `chain`, e.g.: 15 # be_bigger_than(2).and_smaller_than(4).description 16 # # => "be bigger than 2 and smaller than 4" 17 # ...rather than: 18 # # => "be bigger than 2" 19 expectations.include_chain_clauses_in_custom_matcher_descriptions = true 20 end 21 22 # rspec-mocks config goes here. You can use an alternate test double 23 # library (such as bogus or mocha) by changing the `mock_with` option here. 24 config.mock_with :rspec do |mocks| 25 # Prevents you from mocking or stubbing a method that does not exist on 26 # a real object. This is generally recommended, and will default to 27 # `true` in RSpec 4. 28 mocks.verify_partial_doubles = true 29 end 30 31 # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 32 # have no way to turn it off -- the option exists only for backwards 33 # compatibility in RSpec 3). It causes shared context metadata to be 34 # inherited by the metadata hash of host groups and examples, rather than 35 # triggering implicit auto-inclusion in groups with matching metadata. 36 config.shared_context_metadata_behavior = :apply_to_host_groups

spec/system/tasks_spec.rb

ruby

1require 'rails_helper' 2 3 4describe 'タスク管理機能', type: :system do 5 6 describe '一覧表示機能' do 7 before do 8 user_a = FactoryBot.create(:user, name: 'ユーザーA', email: 'a@example.com') 9 FactoryBot.create(:task, name: '最初のタスク', user: user_a) 10 end 11 12 context 'ユーザーAがログインしているとき' do 13 14 before do 15 visit login_path 16 fill_in 'メールアドレス', with: 'a@example.com' 17 fill_in 'パスワード', with: 'password' 18 click_button 'ログインする' 19 end 20 21 it 'ユーザーAが作成したタスクが表示される' do 22 expect(page).to have_content '最初のタスク' 23 end 24 25 end 26 end 27end

関係ありそうな情報

https://qiita.com/jnchito/items/f9c3be449fd164176efa
https://github.com/titusfortner/webdrivers/blob/master/lib/webdrivers/chromedriver.rb
https://github.com/titusfortner/webdrivers/issues/67

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

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

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

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

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

guest

回答1

0

ベストアンサー

著者の松本です。WSL環境に対する考慮が漏れていて申し訳ないです…。

最初のエラーでは、お察しの通りchromedriverから参照できるchromeが見えていないのが原因かと思います。
Ubuntuにchromeを入れたのに Connection refused になるのは、chromedriver-helper がうまく起動していないためかもしれません。

以下のコマンドを実行してみるとどうなるでしょうか?

bundle exec chromedriver-helper& bundle exec rspec

もしこれで通らない場合、下記リンクのように、WSLからWindowsのchromedriverを参照するよう設定する方法も試してみてください。

https://gist.github.com/upinetree/fb71a947cc100e7918b7b280485d620c#対策a-chromedriver-helper-を使わずに自分で-chromedriver-を設定する

投稿2019/05/02 11:12

upinetree

総合スコア29

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

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

Kazuki_Sasaki

2019/05/03 18:48

返信が遅くなり申し訳ございません。 bundle exec chromedriver-helper& bundle exec rspec 上記のコマンドを実行してもエラーになりテストをパスすることができませんでした。 ただ記載していただいたリンクの 対策A. chromedriver-helper を使わずに、自分で chromedriver を設定する の方法を試した結果テストをパスすることが出来ました。 なにが起きているのかを明確に説明することが出来ないです…。
Kazuki_Sasaki

2019/05/04 04:58

Failure/Error: visit login_path Selenium::WebDriver::Error::UnknownError: unknown error: session deleted because of page crash from unknown error: cannot determine loading status from tab crashed (Session info: headless chrome=74.0.3729.131) (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.4.0-17134-Microsoft x86_64) このようなエラーが頻発してしまいます…。
upinetree

2019/05/04 13:52

> 上記のコマンドを実行してもエラーになりテストをパスすることができませんでした。 通常 chromedriver-helper はCapybaraが自動的に起動するようにセットされるのですが、どうも起動が遅れるのか、WSLだとうまくいかない事があるみたいです。 経験的に手動で chromedriver-helper を起動するとうまくいくことがあったのですが、それもエラーになるとすると、他の可能性も含めてもうちょっと深掘りがいるのかもです。 ただ、 `対策A` の方でパスできていたようで良かったです。 こちらの方法はWindows側の chromedriver.exe とChromeを呼び出して、その結果をWSL側のCapybaraで受け取っているような動作になっているためちょっとトリッキーですね…。 > Selenium::WebDriver::Error::UnknownError: > unknown error: session deleted because of page crash > from unknown error: cannot determine loading status > from tab crashed あら…こちらのエラーは `対策A` を実施したあとでしょうか?
Kazuki_Sasaki

2019/05/05 05:19

対策Aを実施した後になります…。 上記のエラー内容と下記のようなエラーも書いてあります。 Failure/Error: e = error Selenium::WebDriver::Error::WebDriverError: invalid session id (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.4.0-17134-Microsoft x86_64) これはもう一度スキップしたほうがいいんでしょうか…?
upinetree

2019/05/05 15:12

なるほどです。 一度はパスしていたのですよね…となると、実行する中で環境の何らかの状態(メモリとか)が変化して、という可能性もありますね。 ↓の記事のように、タスクマネージャに chromedriver がたくさん残っているようなことはないでしょうか? https://boukenki.info/selenium-webdriver-exception-unknown-error-session-deleted-because-of-page-crash-from-tab-crashed-kaiketsu-houhou/ また、↓の回答ではChromeの起動オプションを変更すると改善するかも?と紹介されていますね。 https://stackoverflow.com/questions/53902507/unknown-error-session-deleted-because-of-page-crash-from-unknown-error-cannot 起動オプションを変更する方法は先日紹介したGistの `対策B` に書いてあるので、参照してみてください。 > これはもう一度スキップしたほうがいいんでしょうか…? 書籍の進め方という意味ですよね。 モチベーション的にもつらい状況だと思いますので、一度テストがパスしていることを確認できていますし、学習としてはスキップして次に進んでしまっても良いと思います…!
Kazuki_Sasaki

2019/05/05 17:57

>↓の記事のように、タスクマネージャに chromedriver がたくさん残っているようなことはないでしょうか? タスクマネージャーを確認したところ怪しいプロセスがいくつもあったので終了させました。 その後にテストを実行したところ問題なくパスすることが出来ました! 無事に5章を終わらせることが出来ました。 今後、また同じようなエラーが発生した場合はタスクマネージャーを確認したいと思います。 少ない情報しか提供できていないのに、迅速かつ的確に対応していただき感謝します。
upinetree

2019/05/06 14:43

プロセス自動的に終了しないのは結構罠ですね…無事に動くようになってよかったです!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問