webdriversのエラー
gem webdrivers
をインストールし、RSpecのシステムテストを実行したところ以下のエラーが発生。
NameError: uninitialized constant ActionDispatch::SystemTesting::Browser::Selenium
gem chromedriver-helper
をインストールしたところ解決した。
テスト実行時のブラウザについて
rails generate rspec:system
で作成されるファイルでは driven_by で実行時のブラウザが指定される。
require 'rails_helper' RSpec.describe "Tasks", type: :system do before do driven_by(:rack_test) end pending "add some scenarios (or delete) #{__FILE__}" end
これを一元管理したい場合は他のファイルで設定する。
# spec/support/driver_setting.rb RSpec.configure do |config| config.before(:each, type: :system) do # driven_by(:rack_test) # driven_by(:selenium_chrome) driven_by(:selenium_chrome_headless) end end
ステータスコードを確認したい場合はdriven_by(:rack_test)
を使用する。
Unable to find field "Password confirmation" that is not disabledの解決
下記テストを実行したところ上記エラー発生。
RSpec.describe "Users", type: :system do let(:user) { create(:user) } describe 'ログイン後' do before { sign_in_as(user) } describe 'ユーザー編集' do context 'フォームの入力値が正常' do it 'ユーザーの編集が成功する' do visit edit_user_path(user) fill_in "Email", with: "new-tester@example.com" fill_in "Password", with: "new_password" fill_in "Password confirmation", with: "new_password" click_button "Update" expect(page).to have_content "User was successfully updated." expect(current_path).to eq user_path(user) end end end end end
原因:そもそもsing_in_as(user)
でログインできていなかった。
なぜsing_in_as(user)
は無効だったのか
# spec/support/login_support.rb module LoginSupport def sign_in_as(user) visit login_path fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Login" end end RSpec.configure do |config| config.include LoginSupport end
原因:
fill_in "Password"
でuser.password
を指定してしまったため。
※sorceryでログイン機能を実装していたため、crypted_password
というカラムはあるが、password
というカラムは存在しない。
修正版
module LoginSupport def sign_in_as(user) visit login_path fill_in "Email", with: user.email fill_in "Password", with: "test-password" click_button "Login" end end RSpec.configure do |config| config.include LoginSupport end
特定のテストケースのみを実行
spec_helper.rb
で以下の一行を有効にする。
config.filter_run_when_matching :focus
限定的に実行したいテストケースのit
をfit
に変える。
fit 'is valid with all attributes' do task = build(:task) expect(task).to be_valid expect(task.errors).to be_empty end