RailsApp Rails RSpec Capybara 2018年 12月 3日
integration_test が終わったので,system spec によるフルスタックテストを実施する. 今回も,TechRacho の Rails 5.1以降のシステムテストをRSpecで実行する(翻訳) を参考に処理を進める.
group :development, :test do
# Capybara system testingとselenium driverのサポートを追加
gem 'capybara'
gem 'selenium-webdriver'
# Chromeでのシステムテスト実行に使うchromedriverを簡易インストール
gem 'chromedriver-helper'
(中略)
end
brew cask install chromedriver
Fetching io-like 0.3.0
Fetching public_suffix 3.0.3
Fetching regexp_parser 1.3.0
Installing io-like 0.3.0
Installing regexp_parser 1.3.0
Installing public_suffix 3.0.3
Fetching rubyzip 1.2.2
Installing rubyzip 1.2.2
Fetching archive-zip 0.11.0
Installing archive-zip 0.11.0
Fetching childprocess 0.9.0
Fetching addressable 2.5.2
Installing childprocess 0.9.0
Installing addressable 2.5.2
Fetching xpath 3.2.0
Fetching selenium-webdriver 3.141.0
Installing xpath 3.2.0
Fetching chromedriver-helper 2.1.0
Fetching capybara 3.11.1
Installing chromedriver-helper 2.1.0
Installing selenium-webdriver 3.141.0
Installing capybara 3.11.1
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :selenium_chrome_headless
end
(後略)
end
mkdir -p spec/system
require 'rails_helper'
RSpec.describe :Kurasus, type: :system do
include Devise::Test::IntegrationHelpers
teacher_login :hkob
it 'クラスを作成できること' do
visit kurasus_path
click_on '編集モードに入る'
click_on 'クラス作成'
fill_in 'クラス名', with: '2300'
click_on '登録'
expect(page).to have_content 'クラスを登録しました.'
end
context '2300 クラスが存在する時' do
let!(:kurasu) { kurasu_factory :hkob2300 }
it 'クラスを編集できること' do
visit kurasus_path
click_on '編集モードに入る'
click_on 'クラス編集'
fill_in 'クラス名', with: '3300'
click_on '更新'
expect(page).to have_content 'クラスを更新しました.'
expect(page).to have_content '3300'
end
it 'クラスを削除できること' do
visit kurasus_path
click_on '編集モードに入る'
click_on 'クラス削除'
page.driver.browser.switch_to.alert.accept
expect(page).to have_content 'クラスを削除しました.'
expect(page).not_to have_content '2300'
end
it 'クラスを表示できること' do
visit kurasus_path
click_on 'クラス表示'
expect(page).to have_content '2300'
end
end
end
guard :rspec, cmd: "bundle exec rspec" do
Kurasus
Capybara starting Puma...
* Version 3.12.0 , codename: Llamas in Pajamas
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:52270
クラスを作成できること
2300 クラスが存在する時
クラスを編集できること
クラスを削除できること
クラスを表示できること
長くなったので今日はここまで