[RUBY] Rails learning day 2 part 2

Ruby on Rails5 Quick Learning Practice Guide chapter5

5-4 Types of tests described in this chapter (RSpec)

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.

5-7 Prepare to create test data with FactoryBot

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

  1. Create a template to create data in FactoryBot.
  2. Input test data to the test database using the FactoryBot template, such as before SystemSepc.

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)

5-8 SystemSpec of task list display function

In the above

  1. Create a template to create data in FactoryBot.
  2. Input test data to the test database using the FactoryBot template, such as before SystemSepc. I went to No. 1. Next, the second test is performed using SystemSpec. This time

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

Untitled-1 copy.jpg

5-10 standardization using before

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.

5-11 Commonization using let

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

5-13 Use shared_example

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

Rails learning day 2 part 2
Rails learning day 1 part 3
Rails learning day 3 part 2
Rails learning Day 1 Part 2
Rails learning day 4
Rails learning day 2
rails learning day 1
Programming learning day 3
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
java learning day 2
java learning day 1
Rails Tutorial Chapter 3 Learning
[Rails] Learning with Rails tutorial
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
4th day of java learning
Ruby on Rails basic learning ①
Ruby on rails learning record-2020.10.07 ②
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
Rails 5 Code Reading Part 2 ~ Action View ~
rails learning rake task, cron, whenever
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
part of the syntax of ruby ​​on rails
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
Rails Tutorial 6th Edition Learning Summary Chapter 3
Muscle Ruby on Rails Day 1 ~ Environment Construction ~
Rails Tutorial 6th Edition Learning Summary Chapter 8