-What to do when you launch an application with rails --Display Flash message in Rails -Create your own Rails validate -Implementation of login function with rails
Continuing from now, I will post the output of Ruby on Rails 5 Quick Learning Practice Guide that can be used in the field! This time about rspec! Let's take the task management application as an example.
<!-Edit title and anchor name->
・ Install gem
Gemfile
Add the following to the block of group: development,: test do
.
gem 'rspec-rails', '~> 3.7'
After writing, install the gem with bundle install
. When you're done, run the following command to create the directories and configuration files needed for RSpec.
rails g spec:install
-Delete test directory
Delete the test directory that is automatically created when you launch the application with rails new
. This is because Rspec files are created in the spec directory.
rm -r ./test
-Edit spec_helper.rb to use Capybara
Since Capybara was installed when you did rails new
, describe loading the function
and setting the driver to execute
.
spec/spec_helper.rb
require 'capybara/spec'
config.before(:each, type: :system) do
driven_by :selenium_chrome_headless
end
・ Installation of FactoryBot
Gemfile
Add the following to the block of group: development,: test do
.
gem 'factory_bot_rails', '~> 4.11'
Here is an example of a test for listing task management applications.
tasks_spec.rb
describe 'List display function' do
context 'When Mr. A is logged in' do
before do
#Describe the process so that the test conditions are met
end
it 'Only Mr. A's post is displayed' do
#Describe the expected behavior
end
end
end
In the above example, there is only one condition, but if there are multiple conditions, you can nest the context.
Create spec/factories/users.rb
and describe the data of the User model.
spec/factories/users.rb
FactoryBot.define do
factory :user do
name { 'Test user' }
email { '[email protected]' }
password { 'password' }
end
end
The description of factory: user
interprets rails as test data of the User model. If you want to give it a different name, specify the class as follows.
factory :test_user, class: User do
Next, create the post data associated with the user you just created. As before, create spec/factories/tasks.rb
.
spec/factories/tasks.rb
FactoryBot.define do
factory :task do
name { 'Create a test' }
description { 'Install and create what you need.' }
user
end
end
The above user
is defined to be associated with the data of: user
created earlier. Also here, when associating test data different from the model name, write the user
part as follows.
association :user, factory: :admin_user
First of all, we will create a framework in Japanese.
spec/system/tasks_spec.rb
require 'rails_helper'
describe 'List display function' do
before do
#Create user A
#Create a task for user A
end
context 'When user A is logged in' do
before do
#Login as user A
#Transition to login screen
#Enter your email address
#Enter password
#Press the login button
end
it 'The task created by user A is displayed.'
#The created task is displayed
end
end
end
Once you have a test framework, you can actually write test code!
spec/system/tasks_spec.rb
require 'rails_helper'
describe 'List display function' do
before do
#Create user A
user_a = FactoryBot.create(:user)
#Create a task for user A
FactoryBot.create(:task, name: "First task", user: user_a)
end
context 'When user A is logged in' do
before do
#Login as user A
#Transition to login screen(Visit to path on login screen)
visit login_path
#Enter your email address(Specify the name of the label)
fill_in 'mail address', with: '[email protected]'
#Enter password(Specify the name of the label)
fill_in 'password', with: 'password'
#Press the login button
click_botton 'Login button'
end
it 'The task created by user A is displayed.'
#The created task is displayed
expect(page).to have_content 'First task'
end
end
end
-Ruby on Rails 5 Quick Learning Practice Guide that can be used in the field
Recommended Posts