The system test, which is located in the outermost frame of the test, is important. RSpec is like software that performs System Test In systemtest, software called capybara works as if you are running a web application, and it looks for system errors in the movement.
First of all, when testing using a database, it is not possible to talk without data for testing. The flow is to create test data with FactoryBot and then test from there. Here, let's see how to write specific data in FactoryBot. There are two specific steps required to run the test
spec/factories/users.rb
FactoryBot.define do
factory :user do #①
name {'Test user'}
email {'[email protected]'}
password {'password'}
end
end
I wrote a method called factory and put the value of user information (temporary) in it. This will be the test data. ① factory: 〇〇 do (user this time) This 〇〇 part is the name of this factory and the name used when calling. Basically, use the name of the class corresponding to the created data table (user class corresponds to the user table). By doing so, both the class name and the factory method name can be combined. However, if you really want to use something with a different class name and method name,
factory :admin_user, class: User do
You can also say factory: factory name, class: class name do as above.
Create Task data for testing
spec/factories/tasks.rb
FactoryBot.define do
factory :task do
name {'Write a test'}
description {'RSpec & Capybara &Prepare FactoryBot'}
user #①
end
end
① The above user is for associating task with user (write here which user this created task belongs to)
In the above
When you move to the list screen, the created tasks are displayed.
Write a test code and check if the operation like this works correctly. First of all, you have to write test code to make it work. The test code is
① Create user A (preparation) (2) Create a task whose creator is user A (preparation) ③ Log in from the browser as user A (preparation) ④ Confirm (execute) that the name of the task created by user A is displayed on the screen.
Write these four steps with test code
① Create user A (preparation)
spec/system/tasks_spec.rb
user_a = FactoryBot.create(:user, name: 'username', email:'[email protected]')
I pulled out a factory named user and used that information as user_a. By writing the name and email attached to the back, the content is changed and information only for user A is created (Note that the unchanged part such as password is the value set when the factory method was created)
② Create a task whose creator is user A (preparation)
spec/system/tasks_spec.rb
FactoryBot.create(:task, name: 'First task', user:user_a)
Writing user: user_a means that this task was written by user_a.
③ Log in from the browser as user A (preparation) To log in
A. Go to the login screen B. Enter your email address C. Enter your password D. Press the "Login" button
The operation is necessary.
A. You can go to the login screen by writing visit login_path (visit URL)
spec/system/tasks_spec.rb
visit login_path
B. Enter your email address, C. Use the fill_in method to enter your password
spec/system/tasks_spec.rb
fill_in'mail address', with: '[email protected]'
fill_in'password', with: 'password'
D. Use the click_button method to press the "Login" button
spec/system/tasks_spec.rb
click_button'log in'
To summarize ③
spec/system/tasks_spec.rb
visit login_path
fill_in'mail address', with: '[email protected]'
fill_in'password', with: 'password'
click_button'log in'
It will be like this.
④ Confirm (execute) that the name of the task created by user A is displayed on the screen.
To check, use RSpec's own writing style
expect(page).to have_content 'First task'
expect (page) .to → I expect the screen have_content'First task' → Whether there is content called first task Will mean
Let's combine the above operations into one code ①②③ is a code for preparation, so before ④ is the code for execution, so enter it using it
spec/system/tasks_spec.rb
require 'rails_helper'
describe 'Task management function',type:system do #Type for the most general describe:Attach system
describe 'List display function' do
before do
user_a = FactoryBot.create(:user, name: 'username', email:'[email protected]')
FactoryBot.create(:task, name: 'First task', user:user_a)
end
context'When user A is logged in' do
before do
visit login_path
fill_in'mail address', with: '[email protected]'
fill_in'password', with: 'password'
click_button'log in'
end
it'The task created by user A is displayed.' do
expect(page).to have_content 'First task'
end
end
end
end
Overview of PSpec
Create a pattern in which user B as well as user A is logged in
spec/system/tasks_spec.rb
require 'rails_helper'
describe 'Task management function',type:system do #Type for the most general describe:Attach system
describe 'List display function' do
before do
user_a = FactoryBot.create(:user, name: 'username', email:'[email protected]')
FactoryBot.create(:task, name: 'First task', user:user_a)
end
context'When user A is logged in' do
before do
visit login_path
fill_in'mail address', with: '[email protected]'
fill_in'password', with: 'password'
click_button'log in'
end
it'The task created by user A is displayed.' do
expect(page).to have_no_content 'First task'
end
end
context'When user B is logged in' do
before do
visit login_path
fill_in'mail address', with: '[email protected]'
fill_in'password', with: 'password'
click_button'log in'
end
it'Tasks created by user A are not displayed' do
expect(page).to have_no_content 'First task'
end
end
end
end
visit login_path fill_in'email address', with:'[email protected]' fill_in'password', with:'password' click_button'Login' There are two, which makes it difficult to understand. So I will put this together.
spec/system/tasks_spec.rb
require 'rails_helper'
describe 'Task management function',type:system do #Type for the most general describe:Attach system
describe 'List display function' do
before do
user_a = FactoryBot.create(:user, name: 'username', email:'[email protected]')
FactoryBot.create(:task, name: 'First task', user:user_a)
visit login_path
fill_in'mail address', with: '[email protected]'
fill_in'password', with: 'password'
click_button'log in'
end
context'When user A is logged in' do
it'The task created by user A is displayed.' do
expect(page).to have_content 'First task'
end
end
context'When user B is logged in' do
before do
FactoryBot.create(:user, name: 'username', email:'[email protected]')
end
it'Tasks created by user A are not displayed' do
expect(page).to have_no_content 'First task'
end
end
end
end
When user A is logged in, execute the upper it, and when user B is logged in, execute the lower it.
Data created with FactoryBot can be treated like a variable using let.
spec/system/tasks_spec.rb
require 'rails_helper'
describe 'Task management function',type:system do #Type for the most general describe:Attach system
describe 'List display function' do
let(:user_a){FactoryBot,create(:user, name:'User A', email: '[email protected]')} #User Aを作ったパターン
let(:user_b){FactoryBot,create(:user, name:'User B', email: '[email protected]')} #User Bを作ったパターン
before do
FactoryBot.create(:task, name: 'First task', user:user_a)
visit login_path
fill_in'mail address', with: login_user.email #I don't know if I'm logging in to A or B so I can go either way
fill_in'password', with: login_user.password #I don't know if I'm logging in to A or B so I can go either way
click_button'log in'
end
context'When user A is logged in' do
let(:login_user){user_a} #Extract user A information defined above
it'The task created by user A is displayed.' do
expect(page).to have_content 'First task'
end
end
context'When user B is logged in' do
let(:login_user){user_b} #Extract user B information defined above
it'Tasks created by user A are not displayed' do
expect(page).to have_no_content 'First task'
end
end
end
end
How to put together the results of the same it when executing with different describe
#it operation shared_example_write in for
shared_example_for 'The task created by user A is displayed.' do
it {expect(page).to have_content 'First task'}
end
#it_behaves_You don't have to write it operation once just by writing like
it_behaves_like 'The task created by user A is displayed.'
Recommended Posts