前提・実現したいこと
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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/03 18:48
2019/05/04 04:58
2019/05/04 13:52
2019/05/05 05:19
2019/05/05 15:12
2019/05/05 17:57
2019/05/06 14:43