system spec
gemの導入
group :development, :test do gem 'byebug', platforms: %i[mri mingw x64_mingw] gem 'factory_bot_rails' gem 'rspec-rails', '~> 5.0.0' gem 'webdrivers' gem 'capybara' end
rspecでcapybaraを使うために必要な機能を読み込む
require 'capybara/rspec'
system specのファイルを作成
$ rails g rspec:system tasks Running via Spring preloader in process 2824 create spec/system/tasks_spec.rb
ドライバの設定
ドライバとは、capybaraにおいて、ブラウザの機能を利用するためのもの
spec/support/capybara.rbを作成
$ mkdir spec/support $ touch spec/support/capybara.rb
ブラウザにheadless chromeを使用し、selenium_chrome_headlessをドライバとして指定。
RSpec.configure do |config| config.before(:each, type: :system)do driven_by(:selenium_chrome_headless) end end
モジュールの作成
login用のメソッドをmoduleとして共通化する。
module LoginModule def login(user) visit login_path fill_in 'Email', with: user.email fill_in 'Password', with: 'password' click_button 'Login' end end
includeする。
RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" config.include FactoryBot::Syntax::Methods config.include LoginModule
spec/support以下を読み込ませる。
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
systemspecの設定
require 'rails_helper' RSpec.describe "Users", type: :system do let(:user) { create(:user) } let(:other_user) { create(:user) } describe 'ログイン前' do describe 'ユーザー新規登録' do context 'フォームの入力値が正常' do it 'ユーザーの新規作成が成功する' end context 'メールアドレスが未入力' do it 'ユーザーの新規作成が失敗する' end context '登録済のメールアドレスを使用' do it 'ユーザーの新規作成が失敗する' do existed_user = create(:user) visit sign_up_path fill_in 'Email', with: existed_user.email fill_in 'Password', with: 'password' fill_in 'Password confirmation', with: 'password' click_button 'SignUp' expect(page).to have_content '1 error prohibited this user from being saved' expect(current_path).to eq users_path expect(page).to have_content("Email has already been taken") expect(page).to have_field 'Email', with: existed_user.email end end end end describe 'ログイン後' do before { login(user) } describe 'ユーザー編集' do context 'フォームの入力値が正常' do it 'ユーザーの編集が成功する' end context 'メールアドレスが未入力' do it 'ユーザーの編集が失敗する' end context '登録済みのメールアドレスを使用' do it 'ユーザーの編集が失敗する' do visit edit_user_path(user) fill_in 'Email', with: other_user.email fill_in 'Password', with: 'password' fill_in 'Password confirmation', with: 'password' click_button 'Update' expect(page).to have_content('1 error prohibited this user from being saved') expect(page).to have_content("Email has already been taken") expect(current_path).to eq user_path(user) end end context '他ユーザーの編集ページにアクセス' do it '編集ページへのアクセスが失敗する' do visit edit_user_path(other_user) expect(current_path).to eq user_path(user) expect(page).to have_content("Forbidden access.") end end end describe 'マイページ' do context 'タスクを作成' do it '新規作成したタスクが表示される' do create(:task, title:'test', status: :doing, user: user) visit user_path(user) expect(page).to have_content('You have 1 task.') expect(page).to have_content('test') expect(page).to have_content('doing') expect(page).to have_link('Show') expect(page).to have_link('Edit') expect(page).to have_link('Destroy') end end end end end
require 'rails_helper' RSpec.describe 'Tasks', type: :system do let(:user) { create(:user) } let(:task) { create(:task) } describe 'ログイン前' do describe 'ページ遷移確認' do context 'タスクの新規登録ページにアクセス' do it '新規登録ページへのアクセスが失敗する' do visit new_task_path expect(page).to have_content('Login required') expect(current_path).to eq login_path end end context 'タスクの編集ページにアクセス' do it '編集ページへのアクセスが失敗する' do visit edit_task_path(task) expect(page).to have_content('Login required') expect(current_path).to eq login_path end end context 'タスクの詳細ページにアクセス' do it 'タスクの詳細情報が表示される' do visit task_path(task) expect(page).to have_content task.title expect(current_path).to eq task_path(task) end end context 'タスクの一覧ページにアクセス' do it 'すべてのユーザーのタスク情報が表示される' do task_list = create_list(:task, 3) visit tasks_path expect(page).to have_content task_list[0].title expect(page).to have_content task_list[1].title expect(page).to have_content task_list[2].title expect(current_path).to eq tasks_path end end end end describe 'ログイン後' do before { login(user) } describe 'タスク新規登録' do context 'フォームの入力値が正常' do it 'タスクの新規作成が成功する' do visit new_task_path fill_in 'Title', with: 'test_title' fill_in 'Content', with: 'test_content' select 'doing', from: 'Status' fill_in 'Deadline', with: DateTime.new(2020, 6, 1, 10, 30) click_button 'Create Task' expect(page).to have_content 'Title: test_title' expect(page).to have_content 'Content: test_content' expect(page).to have_content 'Status: doing' expect(page).to have_content 'Deadline: 2020/6/1 10:30' expect(current_path).to eq '/tasks/1' end end context 'タイトルが未入力' do it 'タスクの新規作成が失敗する' do visit new_task_path fill_in 'Title', with: '' fill_in 'Content', with: 'test_content' click_button 'Create Task' expect(page).to have_content '1 error prohibited this task from being saved:' expect(page).to have_content "Title can't be blank" expect(current_path).to eq tasks_path end end context '登録済のタイトルを入力' do it 'タスクの新規作成が失敗する' do visit new_task_path other_task = create(:task) fill_in 'Title', with: other_task.title fill_in 'Content', with: 'test_content' click_button 'Create Task' expect(page).to have_content '1 error prohibited this task from being saved' expect(page).to have_content 'Title has already been taken' expect(current_path).to eq tasks_path end end end describe 'タスク編集' do let!(:task) { create(:task, user: user) } let(:other_task) { create(:task, user: user) } before { visit edit_task_path(task) } context 'フォームの入力値が正常' do it 'タスクの編集が成功する' do fill_in 'Title', with: 'updated_title' select :done, from: 'Status' click_button 'Update Task' expect(page).to have_content 'Title: updated_title' expect(page).to have_content 'Status: done' expect(page).to have_content 'Task was successfully updated.' expect(current_path).to eq task_path(task) end end context 'タイトルが未入力' do it 'タスクの編集が失敗する' do fill_in 'Title', with: nil select :todo, from: 'Status' click_button 'Update Task' expect(page).to have_content '1 error prohibited this task from being saved' expect(page).to have_content "Title can't be blank" expect(current_path).to eq task_path(task) end end context '登録済のタイトルを入力' do it 'タスクの編集が失敗する' do fill_in 'Title', with: other_task.title select :todo, from: 'Status' click_button 'Update Task' expect(page).to have_content '1 error prohibited this task from being saved' expect(page).to have_content "Title has already been taken" expect(current_path).to eq task_path(task) end end end describe 'タスク削除' do let!(:task) { create(:task, user: user) } it 'タスクの削除が成功する' do visit tasks_path click_link 'Destroy' expect(page.accept_confirm).to eq 'Are you sure?' expect(page).to have_content 'Task was successfully destroyed' expect(current_path).to eq tasks_path expect(page).not_to have_content task.title end end end end