A memorandum when developing an application with Ruby on Rails and then using it for another application (Mac as of October 21, 2020)
It took me a long time to get started, so please try it if you like to get a feel for it.
Gemfile
group :test do
gem 'capybara', '>= 2.15'
gem 'rspec-rails'
gem "factory_bot_rails"
gem 'faker'
end
Terminal
bundle install
rspec-rails Gem to run Rspec Ruby on Rails test framework RSpec getting started https://qiita.com/tatsurou313/items/c923338d2e3c07dfd9ee
capybara Rspec didn't work without driver settings, so I introduced Gem Capybara cheat sheet https://qiita.com/morrr/items/0e24251c049180218db4
faker Gem for creating data. Mainly the screen when executing Rspec is input test test data is randomly created using the library in Gem. Temporary data can also be entered in the seed file, which is convenient. I tried using Faker! (Usage and execution example) https://qiita.com/ginokinh/items/3f825326841dad6bd11e
factory_bot_rails I used it for debugging Gem, which can create test data first, mainly for Model. [Rails] Notes on how to use factory_bot https://qiita.com/at-946/items/aaff42e4a9c2e4dd58ed
Terminal
rails generate rspec:install
The following files will be created when installed.
Terminal
Running via Spring preloader in process 9045
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
I don't understand how it works, and probably thanks to the settings here, I can run the program in the same environment as the browser.
spec_helper.rb
require 'capybara/rspec'
RSpec.configure do |config|
config.before(:each, type: :system) do
#driven_by :selenium_chrome_headless
driven_by :rack_test
end
end
You can create data to be created in the factories folder, model can test validation etc., and sysyem can test the actual operation on the screen. Code is shown for reference
factories/users_spec.rb
FactoryBot.define do
factory :user do
email { Faker::Internet.email }
password { 'password' }
password_confirmation { 'password' }
end
end
model/user_spec.rb
require 'rails_helper'
RSpec.describe 'User model testing', type: :model do
before do
@user = build(:user)
end
describe 'Validation' do
it 'NG if email is empty' do
@user.email = ''
expect(@user.valid?).to eq(false)
end
end
end
system/user_spec.rb
require 'rails_helper'
describe 'User authentication test' do
describe 'New user registration' do
before do
visit new_user_registration_path
end
context 'Transition to new registration screen' do
it 'Successful new registration' do
fill_in 'user[email]', with: Faker::Internet.email
fill_in 'user[password]', with: 'password'
fill_in 'user[password_confirmation]', with: 'password'
click_button "sign in" #The character displayed on the button
expect(page).to have_content 'successful' #Please specify the message that will be displayed after login
end
end
end
end
All the tests in the spec folder are executed by the following command, and the successful ones are displayed in green and the unsuccessful ones are displayed in red on the terminal.
Terminal
bundle exec rspec spec/ --format documentation
Thank you for your hard work.
Recommended Posts