[2nd] RSpec beginners wrote SystemSpec as a beginner (swamp edition)

Introduction

This is the previous article [1st] RSpec beginner wrote ModelSpec as a beginner. If you want to know about ModelSpec, please go to the previous article!

Also, the other day, a study session for beginners (RSpec Beginners !!) was held with the kindness of RSpec male Junichi Ito @jnchito. I also participated in "RSpec Beginners !!"), so I think that you can deepen your understanding by watching this video, so please have a look if you like.

What to do in this article

Not covered in this article

Premise

About system specifications

What is ** system spec ** in the first place? : thinking: I think some people say.

Imagine, for example, that you use a website. Registering an account, logging in, editing my page, posting something, sending a DM to someone, etc., to name a few, take some action on the actual browser. right? The system specs test that this ** user action on the actual browser works correctly **. That's important because it targets things that users actually move while viewing the website. Therefore, at the end of the previous article, I mentioned that it is important to write model specs, but it is important to write system specs.

Also, if you're looking for an RSpec article, you'll also see ** "feature specs" **, but think of it as the predecessor of system specs. Starting with RSpec 3.7, you can add system specs to your test suite for Rails 5.1 and later applications, so if you're using a version above or later, let's write the system specs. ..

By the way, various settings are required to describe the system specifications, so please refer to Everyday Rails for the required gems and settings.

Testing users_spec.rb

① Use FactoryBot and prepare user data in advance

Last time, I used FactoryBot to prepare the preliminary data. It is also used when writing system specifications, so it is listed below for reference. For more information on FactoryBot, please refer to the previous article.

①spec/factories/users.rb


FactoryBot.define do
  #Use FactoryBot and prepare user data in advance
  factory :user do
    last_name { "test" }
    first_name { "Taro" }
    kana_last_name { "test" }
    kana_first_name { "Taro" }
    email { "[email protected]" }
    postal_code { "1234567" }
    address { "123, Chiyoda-ku, Tokyo-12-1" }
    phone_number { "12345678910" }
    password { "testtaro" }
  end
end

② Specific code description

Now let's write the test code. Running $ rails g rspec: system users will create users_spec.rb in the spec / system folder. The test code (actual movement on the browser, etc.) will be written in this file. Below is a completed example.

②spec/system/users_spec.rb


require 'rails_helper'

RSpec.describe 'User', type: :system do
  let(:user){FactoryBot.create(:user)}
  describe 'User authentication test' do
    describe 'New user registration' do
      before do
        visit new_user_registration_path #Transition to new registration screen
      end
      context 'Transition to new registration screen' do
        it 'Successful new registration' do
          # fill_Enter the registration information in the text box with in
          fill_in 'name(Surname)', with: "test"
          fill_in 'name(Name)', with: "Jiro"
          fill_in 'Frigana(Surname)', with: "test"
          fill_in 'Frigana(Mei)', with: "Jiro"
          fill_in 'mail address', with: "[email protected]"
          fill_in 'Postal code(no hyphen)', with: "2222222"
          fill_in 'Street address', with: "123 Adachi-ku, Tokyo-12-1"
          fill_in 'phone number(no hyphen)', with: "22222222222"
          fill_in 'password', with: "testjiro"
          fill_in 'Confirmation password', with: "testjiro"
          click_button 'sign up' #Click the button
          expect(page).to have_content 'Account registration is complete.'
        end
        
        it 'New registration fails' do
          fill_in 'name(Surname)', with: ""
          fill_in 'name(Name)', with: ""
          fill_in 'Frigana(Surname)', with: ""
          fill_in 'Frigana(Mei)', with: ""
          fill_in 'mail address', with: ""
          fill_in 'Postal code(no hyphen)', with: ""
          fill_in 'Street address', with: ""
          fill_in 'phone number(no hyphen)', with: ""
          fill_in 'password', with: ""
          fill_in 'Confirmation password', with: ""
          click_button 'sign up'
          expect(page).to have_content 'Individual members were not saved.'
        end
      end
    end
    
    describe 'User login' do
      before do
        visit new_user_session_path
      end

      context 'Transition to login screen' do
        it 'Login successfully' do
          fill_in "mail address", with: user.email
          fill_in 'password', with: user.password
          click_button 'Login'
          expect(page).to have_content 'You are now logged.'
        end

        it 'Login fails' do
          fill_in 'mail address', with: ''
          fill_in 'password', with: ''
          click_button 'Login'
          expect(current_path).to eq new_user_session_path
        end
      end
    end
  end

  describe 'User testing' do
    before do
      visit new_user_session_path
      fill_in 'mail address', with: user.email
      fill_in 'password', with: user.password
      click_button 'Login'
    end

    describe 'Test my page' do
      it 'My Page is displayed in the header' do
        expect(page).to have_content('My page')
      end
      it 'Go to My Page and the edit link is displayed' do
        visit user_path(user)
        expect(page).to have_content('To edit')
      end
    end

    describe 'Editing test' do
      context 'Transition to edit screen' do
        it 'Can transition' do
          visit edit_user_path(user)
          expect(current_path).to eq edit_user_path(user)
        end
      end

      context 'Check and edit the display' do
        before do
          visit edit_user_path(user)
        end
        it 'It is displayed as "Edit registration information"' do
          expect(page).to have_content('Edit registration information')
        end
        it 'Image editing form is displayed' do
          expect(page).to have_field 'user[profile_image]'
        end
        it 'My surname is displayed on the name edit form' do
          expect(page).to have_field 'name(Surname)', with: user.last_name
        end
        it 'Your name appears in the name edit form' do
          expect(page).to have_field 'name(Name)', with: user.first_name
        end
        it 'My Kana surname is displayed on the name edit form' do
          expect(page).to have_field 'Frigana(Surname)', with: user.kana_last_name
        end
        it 'Your Kana name is displayed in the name edit form' do
          expect(page).to have_field 'Frigana(Mei)', with: user.kana_first_name
        end
        it 'Your email address is displayed in the email address edit form' do
          expect(page).to have_field 'mail address', with: user.email
        end
        it 'Your zip code is displayed on the zip code edit form' do
          expect(page).to have_field 'Postal code(no hyphen)', with: user.postal_code
        end
        it 'Your address is displayed on the address edit form' do
          expect(page).to have_field 'Street address', with: user.address
        end
        it 'Your phone number is displayed in the phone number edit form' do
          expect(page).to have_field 'phone number(no hyphen)', with: user.phone_number
        end
        it 'Your self-introduction text is displayed on the self-introduction text edit form' do
          expect(page).to have_field 'Self-introduction', with: user.introduction
        end
        it 'Successful editing' do
          #Changed the name to Saburo
          fill_in 'name(Name)', with: 'Saburo'
          fill_in 'Frigana(Mei)', with: 'Sabrow'
          click_button 'Save your changes'
          expect(page).to have_content 'The update of member information is complete.'
          expect(page).to have_content 'Test Saburo(Test subrow)'
          expect(current_path).to eq user_path(user)
        end
        it 'Editing fails' do
          # first_name name(Name)Enter in the blank
          fill_in 'name(Name)', with: ''
          click_button 'Save your changes'
          expect(page).to have_content 'The individual member was not saved due to the error.'
          expect(page).to have_content 'name(Name)Please enter'
          expect(current_path).to eq user_path(user)
        end
      end
    end
  end
  
end

For those who wondered, "Oh, the way FactoryBot is written is different from the last time: thinking: ** let (: user) ** ??"

RSpec has a feature called ** let **. You can use this to replace an instance variable in the form let (: variable) and use it as a variable in later code. Please refer to this article for details. When do you use RSpec let? (Translation) Compared to enclosing it in the before block, the amount of code is smaller and it looks more like RSpec, so let's use it. However, as described in the reference article, let has the feature of ** "lazy evaluation" **, so you need to be careful. This will be explained in the code example below. (This is why I got hooked on the swamp)


Let's start with the user authentication test. See the code below.

②spec/system/users_spec.rb


let(:user){FactoryBot.create(:user)}
  describe 'User authentication test' do
    describe 'New user registration' do
      before do
        visit new_user_registration_path #Transition to new registration screen
      end
      context 'Transition to new registration screen' do
        it 'Successful new registration' do
          # fill_Enter the registration information in the text box with in
          fill_in 'name(Surname)', with: "test"
          fill_in 'name(Name)', with: "Jiro"
          fill_in 'Frigana(Surname)', with: "test"
          fill_in 'Frigana(Mei)', with: "Jiro"
          fill_in 'mail address', with: "[email protected]"
          fill_in 'Postal code(no hyphen)', with: "2222222"
          fill_in 'Street address', with: "123 Adachi-ku, Tokyo-12-1"
          fill_in 'phone number(no hyphen)', with: "22222222222"
          fill_in 'password', with: "testjiro"
          fill_in 'Confirmation password', with: "testjiro"
          click_button 'sign up' #Click the button
          expect(page).to have_content 'Account registration is complete.'
        end

First, there is a description of visit new_user_registration_path in the before block. This ** visit ** is a feature of ** Capybara ** that allows you to go to a specific page with visit + path. ** Capybara ** is a convenient function that allows you to transition between various pages as if the user is actually using the website and check if there are any problems at that time (Capybara is various pages). Imagine running around and looking up).

With Capybara, you can use various useful functions other than visit. I think that gem'capybara' is attached by default when creating a Rails application, but please check it. Reference article: [Introduction to RSpec that can be used, part 4 "Any browser operation is free! Reverse lookup Capybara dictionary"](https://qiita.com/jnchito/items/607f956263c38a5fec24 "Introduction to RSpec that can be used, part 4" Any browser operation Also free! Reverse lookup Capybara Encyclopedia "")


Now, from here, I will write the test code that succeeds in new registration while imagining the actual screen.
Yes, I have written a test with system specs that succeeds in new registration! Let's run $ bundle exec rspec spec / system / users_spec.rb in the terminal! If done well, the result * 1 examples, 0 failures * will be output to the terminal. If you get an error, read the error statement and check if there are any mistakes in the code or if there are any omissions at the time of setting. Also, once you get used to it, ʻexpect (page) .not_to have_content' account registration is complete. It's also important to try changing after expect (page) to not_to, like'`, and reconfirm ** "test fails properly" **. If you have written the wrong test code, the test may succeed even though you have intentionally set it to not_to. To prevent this, change it to not_to etc. and make sure that the test fails.

The basics are the same for logging in and editing registration information on My Page, so please refer to this when writing!

By the way, click_button is used for submit button and button tag. On the other hand, if it looks the same by CSS but it is a link, use click_link. Click_on can be used with either. Then, click_on is all right, right? I thought you there. I also thought: hugging: To explicitly separate the button for sending data and the link for transitions? I wondered if it was like that, but is there any other merit in dividing it? I want you to tell me the details.

Testing companies_spec.rb

① Use FactoryBot and prepare company data in advance

It was created last time.

①spec/factories/companies.rb


FactoryBot.define do
  factory :company do
    company_name { "Test Co., Ltd." }
    kana_company_name { "Test Co., Ltd." }
    email { "[email protected]" }
    postal_code { "1234567" }
    address { "123, Chiyoda-ku, Tokyo-12-1" }
    phone_number { "12345678910" }
    password { "testcompany" }
    approved { true }
    is_active { true }
  end
end

② Specific code description

New registration of a corporation is different from new registration of an individual member, and the flow is as follows.

  1. The corporation fills out the form for new registration and clicks the Apply button.
  2. You will be redirected to the top screen with the message "Please wait for a while until you receive the approved email." At the same time, an application notification is sent to the administrator.
  3. Corporate login is restricted until the approved email arrives.
  4. The administrator confirms the application notification and updates the application status to approved. At the same time, an approved email is sent to the registered address of the corporation.
  5. You will be able to log in.

#### Bacon lettuce egg Tsukune rice burger!
I was upset. Cream Stew Ueda-san's tsukkomi about things that are too complicated has come out.

The basics are the same as before, so it wouldn't be a problem if I got used to it, but it was difficult for beginners and I had a hard time. Also, I couldn't find a reference article for such a mechanism, which was the reason for writing this article. (Maybe it's just a bad way to find it) If you are a beginner who has created a similar mechanism, I hope this will be helpful.

Below is an example of the completed code.

②spec/system/companies_spec.rb


require 'rails_helper'

RSpec.describe "Companies", type: :system do
  let!(:admin){FactoryBot.create(:admin)}
  let(:company){FactoryBot.create(:company)}
  describe 'User authentication test' do
    describe 'Application for new registration of corporation' do
      before do
        visit new_company_registration_path
      end
      it 'Successful registration application' do
        fill_in 'Company name', with: "Test 2 Co., Ltd."
        fill_in 'Frigana', with: "Test2 Co., Ltd."
        fill_in 'mail address', with: "[email protected]"
        fill_in 'Postal code(no hyphen)', with: "2222222"
        fill_in 'Street address', with: "222 Chiyoda-ku, Tokyo-22-2"
        fill_in 'phone number(no hyphen)', with: "22222222222"
        fill_in 'password', with: "test2company"
        fill_in 'Confirmation password', with: "test2company"
        click_button 'Apply'
        expect(page).to have_content 'Thank you for your registration application. The corporate members-only page will be available after the application is approved by the operation. Please wait for a while until you receive the approved email.'
      end
      it 'Registration application fails' do
        fill_in 'Company name', with: ""
        fill_in 'Frigana', with: ""
        fill_in 'mail address', with: ""
        fill_in 'Postal code(no hyphen)', with: ""
        fill_in 'Street address', with: ""
        fill_in 'phone number(no hyphen)', with: ""
        fill_in 'password', with: ""
        fill_in 'Confirmation password', with: ""
        click_button 'Apply'
        expect(page).to have_content "Corporate members were not saved."
      end
    end
  end

  describe 'Test until the corporation can log in' do
    before do
      #Corporation fills out registration application form
      visit new_company_registration_path
      fill_in 'Company name', with: "Test 2 Co., Ltd."
      fill_in 'Frigana', with: "Test2 Co., Ltd."
      fill_in 'mail address', with: "[email protected]"
      fill_in 'Postal code(no hyphen)', with: "2222222"
      fill_in 'Street address', with: "222 Chiyoda-ku, Tokyo-22-2"
      fill_in 'phone number(no hyphen)', with: "22222222222"
      fill_in 'password', with: "test2company"
      fill_in 'Confirmation password', with: "test2company"
      click_button 'Apply' #Notification will be sent
    end
    
    describe 'Administrator: Confirmation of notification-Test of application approval' do
      before do
        #Login as administrator
        visit new_admin_session_path
        fill_in 'mail address', with: admin.email
        fill_in 'password', with: admin.password
        click_button 'Login'
      end
      it 'Corporate registration application is displayed in the header' do
        expect(page).to have_content('Corporate registration application')
      end
      it 'The name of the corporation you applied for is displayed in the corporate registration application list.' do
        visit admin_notifications_path
        expect(page).to have_content("Test 2 There is a corporate registration application from Co., Ltd.")
      end
      it 'You can move to the company details page from the link' do
        visit admin_notifications_path
        notification = Notification.find_by({receiver_id: admin.id, receiver_class: "admin", sender_id: Company.last.id, sender_class: "company"})
        within '.request-message' do
          click_link 'Corporate registration application' # ページに同一の文言のリンクがある場合(今回の場合「Corporate registration application」)、classを指定してあげてwithin囲む
        end
        expect(current_path).to eq admin_company_path(notification.sender_id)
      end
      it 'Transition to the edit screen' do
        visit admin_company_path(Company.last.id)
        click_link 'To edit'
        expect(current_path).to eq edit_admin_company_path(Company.last.id)
      end
      it 'Make application status approved' do
        visit edit_admin_company_path(Company.last.id)
        choose "company_approved_true" #Check application status as approved (company)_approved_true is the id of the radio button element)
        click_button 'Save your changes'
        expect(page).to have_content 'The update of company information is complete.'
        expect(current_path).to eq admin_company_path(Company.last.id)
      end
    end

    describe 'Corporate: Login test' do
      context 'Corporate login before approval' do
        it 'Login fails and you are prompted to log in again after receiving the email' do
          visit new_company_session_path
          fill_in 'mail address', with: "[email protected]"
          fill_in 'password', with: "test2company"
          click_button 'Login'
          expect(page).to have_content 'The registration application has not been approved. We're sorry, but please wait for a while until you receive the approved email.'
        end
      end

      context 'Corporate login after approval' do
        before do
          login_as(admin) #Admin login
          visit edit_admin_company_path(Company.last.id)
          choose "company_approved_true" #Check application status as approved (company)_approved_true is the id of the radio button element)
          click_button 'Save your changes'
          click_on 'Log out'
          visit new_company_session_path
        end
        it 'Login successfully' do
          fill_in 'mail address', with: "[email protected]"
          fill_in 'password', with: "test2company"
          click_button 'Login'
          expect(page).to have_content 'You are now logged.'
        end
        it 'Login fails' do
          fill_in 'mail address', with: ""
          fill_in 'password', with: ""
          click_button 'Login'
          expect(current_path).to eq new_company_session_path
        end
      end
    end
  end
    
  describe 'Corporate member test' do
    before do
      visit new_company_session_path
      fill_in 'mail address', with: company.email
      fill_in 'password', with: company.password
      click_button 'Login'
    end

    describe 'Test my page' do
      it 'My Page is displayed in the header' do
        expect(page).to have_content('My page')
      end
      it 'Go to My Page and the edit link is displayed' do
        visit corporate_company_path(company)
        expect(page).to have_content('To edit')
      end
    end

    describe 'Editing test' do
      before do
        visit edit_corporate_company_path(company)
      end
      context 'Confirm transition to edit screen' do
        it 'Can transition' do
          expect(current_path).to eq edit_corporate_company_path(company)
        end
      end
      context 'Confirmation of display and editing' do
        it 'It is displayed as "Edit registration information"' do
          expect(page).to have_content('Edit registration information')
        end
        it 'Profile image editing form is displayed' do
          expect(page).to have_field 'company[profile_image]'
        end
        it 'The header image editing form is displayed' do
          expect(page).to have_field 'company[background_image]'
        end
        it 'The company name is displayed on the company name edit form' do
          expect(page).to have_field 'Company name', with: company.company_name
        end
        it 'Your company Kana name is displayed on the Frigana edit form' do
          expect(page).to have_field 'Frigana', with: company.kana_company_name
        end
        it 'Your email address is displayed in the email address edit form' do
          expect(page).to have_field 'mail address', with: company.email
        end
        it 'Your zip code is displayed on the zip code edit form' do
          expect(page).to have_field 'Postal code(no hyphen)', with: company.postal_code
        end
        it 'Your address is displayed on the address edit form' do
          expect(page).to have_field 'Street address', with: company.address
        end
        it 'Your phone number is displayed in the phone number edit form' do
          expect(page).to have_field 'phone number(no hyphen)', with: company.phone_number
        end
        it 'Your self-introduction text is displayed on the self-introduction text edit form' do
          expect(page).to have_field 'Self-introduction', with: company.introduction
        end
        it 'Successful editing' do
          fill_in 'Self-introduction', with: "Welcome to My Page of Test Co., Ltd.!"
          click_button 'Save your changes'
          expect(page).to have_content 'The update of company information is complete.'
          expect(current_path).to eq corporate_company_path(company)
        end
        it 'Editing fails' do
          fill_in 'Company name', with: ""
          click_button 'Save your changes'
          expect(page).to have_content 'The corporate member was not saved due to the error.'
        end
      end
    end
  end
end

First, create an administrator in FactoryBot first because the administrator must approve the application before the legal entity can log in. Run $ bin / rails g factory_bot: model admin and put in the sample data you need. Below is a file containing sample data.

spec/factories/admins.rb


FactoryBot.define do
  factory :admin do
    email { "[email protected]" }
    password { "testadmin"}
  end
end

The new registration application for the corporation and the final registration information editing test are almost the same as those in users_spec.rb, so explanations are omitted.


In the meantime, I will explain the ** test until the corporation can log in **. See the code below.

②spec/system/companies_spec.rb


  describe 'Test until the corporation can log in' do
    before do
      # 1.Corporation fills out registration application form
      visit new_company_registration_path
      fill_in 'Company name', with: "Test 2 Co., Ltd."
      fill_in 'Frigana', with: "Test2 Co., Ltd."
      fill_in 'mail address', with: "[email protected]"
      fill_in 'Postal code(no hyphen)', with: "2222222"
      fill_in 'Street address', with: "222 Chiyoda-ku, Tokyo-22-2"
      fill_in 'phone number(no hyphen)', with: "22222222222"
      fill_in 'password', with: "test2company"
      fill_in 'Confirmation password', with: "test2company"
      click_button 'Apply' #Notification will be sent
    end
    
    describe 'Administrator: Confirmation of notification-Test of application approval' do
      before do
        # 2.Login as administrator
        visit new_admin_session_path
        fill_in 'mail address', with: admin.email
        fill_in 'password', with: admin.password
        click_button 'Login'
      end
      # 3.Confirm that the administrator has logged in because there is a wording of corporate registration application
      it 'Corporate registration application is displayed in the header' do
        expect(page).to have_content('Corporate registration application')
      end
      # 4.Click the corporate registration application link and confirm the application company name
      it 'The name of the corporation you applied for is displayed in the corporate registration application list.' do
        visit admin_notifications_path
        expect(page).to have_content("Test 2 There is a corporate registration application from Co., Ltd.")
      end
      # 5.Click the link "Application for corporate registration" from the application list
      it 'You can move to the company details page from the link' do
        visit admin_notifications_path
        notification = Notification.find_by({receiver_id: admin.id, receiver_class: "admin", sender_id: Company.last.id, sender_class: "company"})
        within '.request-message' do
          click_link 'Corporate registration application' # ページに同一の文言のリンクがある場合(今回の場合「Corporate registration application」)、classを指定してあげてwithin囲む
        end
        expect(current_path).to eq admin_company_path(notification.sender_id)
      end
      # 6.To the registration information edit page
      it 'Transition to the edit screen' do
        visit admin_company_path(Company.last.id)
        click_link 'To edit'
        expect(current_path).to eq edit_admin_company_path(Company.last.id)
      end
      # 7.Pay attention to the radio buttons
      it 'Make application status approved' do
        visit edit_admin_company_path(Company.last.id)
        choose "company_approved_true" #Check application status as approved (company)_approved_true is the id of the radio button element)
        click_button 'Save your changes'
        expect(page).to have_content 'The update of company information is complete.'
        expect(current_path).to eq admin_company_path(Company.last.id)
      end
    end

    describe 'Corporate: Login test' do
      context 'Corporate login before approval' do
        it 'Login fails and you are prompted to log in again after receiving the email' do
          visit new_company_session_path
          fill_in 'mail address', with: "[email protected]"
          fill_in 'password', with: "test2company"
          click_button 'Login'
          expect(page).to have_content 'The registration application has not been approved. We're sorry, but please wait for a while until you receive the approved email.'
        end
      end

      context 'Corporate login after approval' do
        before do
          login_as(admin) #Admin login
          visit edit_admin_company_path(Company.last.id)
          choose "company_approved_true" #Check application status as approved (company)_approved_true is the id of the radio button element)
          click_button 'Save your changes'
          click_on 'Log out'
          visit new_company_session_path
        end
        it 'Login successfully' do
          fill_in 'mail address', with: "[email protected]"
          fill_in 'password', with: "test2company"
          click_button 'Login'
          expect(page).to have_content 'You are now logged.'
        end
        it 'Login fails' do
          fill_in 'mail address', with: ""
          fill_in 'password', with: ""
          click_button 'Login'
          expect(current_path).to eq new_company_session_path
        end
      end
    end
  end

I said, "Okay, the file was also created ** let (: admin) {FactoryBot.create (: admin)} ** and ... now you can use the variable admin!"

The above description, which seems to be okay at first glance, was the entrance to the swamp.

  1. ** Corporation fills out registration application form ** In the before block, the corporation fills in the registration application form and describes up to the point where the application button is pressed. At this point, the application notification has been sent to the administrator.

  2. ** Stumble point: hugging: Login as administrator "let lazy evaluation" ** I said, "Okay, first log in as an administrator to approve the application ... that? Somehow the test fails, why ... ??" I couldn't find a reason for the test to fail here for a while and fell into a negative spiral. If you look closely, you can see the description of let (: admin) {FactoryBot.create (: admin)}, and the noteworthy part is ** let (: admin) **. At first glance, there seems to be nothing wrong with it, but as I mentioned earlier, let has the characteristic of ** "lazy evaluation" **. What is lazy evaluation? If you are: thinking :, please read this article. Be careful when creating with RSpec let ~ Lazy evaluation ~ When do you use RSpec let? (Translation) According to this article, when you create with just let, the assigned variable is evaluated at the timing when it is referenced = lazy evaluation. In other words, unless the variable admin is used, such as fill_in'email address', with: admin.email, the data content of admin is considered to be nonexistent. I see, but I thought I was writing admin, but when I tried ** let! **, the test passed successfully. Hmmm, I passed, but I wonder why ... I thought it was because some of the describes overlapped, but I didn't understand after all. .. If you are familiar with this, please let me know. ..

  3. ** Confirm that the administrator has logged in because there is a wording of corporate registration application ** There is a link * Corporate registration application * in the header after logging in as an administrator, so make sure that it is displayed and confirm that you have logged in.

  4. ** Click the link for corporate registration application and confirm the name of the applicant company ** When you click the link in the header, the company name will be displayed, so check if it is the company you applied for in 1.

  5. ** Stumble point: hugging: "Click the link" Apply for corporate registration from the application list "** I stumbled here. What you should pay attention to is the part surrounded by ** within'.request-message' do **. What I want to do now is to click the link "Application for corporate registration" and move to the company details page. Normally you should be able to transition with click_link or click_on. However, it didn't work and I got an error. Here is the error statement at this time.

Capybara::Ambiguous: Ambiguous match, found 2 elements matching visible link "Corporate registration application"

To summarize this ** Capybara "Oh, there are two links called" Corporate registration application ", which one should I go to?" ** It means that · · · I noticed here.

法人登録申請 There are two ... I'm sorry Capybara ... As a result of googled the improvement method, specify the id name (request_message) unique to the target part of the view instead of ~~ click_link, and the test code is ** find ("# request_message"). Click ** I solved it with the description. Specify the id and click on the target person. ~~ ** Added on October 01, 2020 ** As advised in the comment, it was solved with within. This is better because it is a click-like form that looks like it in the browser!

②spec/system/companies_spec.rb


within '.request-message' do
  click_link 'Corporate registration application' # ページに同一の文言のリンクがある場合(今回の場合「Corporate registration application」)、classを指定してあげてwithinで囲む
end
<td class="text-center request-message"> #Request to the target part of the view-Specify a class called message. Target here
  <strong><%= notification.sender_name %></strong>From<%= link_to 'Corporate registration application', admin_company_path(notification.sender_id) %>there is. Please check your registration details.
  <%= " (#{time_ago_in_words(notification.created_at)}Before)" %>
</td>
  1. ** Go to registration information edit page ** It's easy here. I will not explain in particular.

  2. ** Stumble point: hugging: "Watch out for radio buttons" ** Use Capybara's choose feature to check your application status as approved. Write in the form choose" company_approved_true ". Where did "company_approved_true" come from? If you use google's verification tool, you can see that the id name is assigned in the input tag. The radio button is in the form of selecting by specifying its id. However, when looking at other articles, there are also articles that select the character string in the label tag as the target. In this case, it's the string "approved". When viewing on a browser, you would normally select by looking at the character string. Therefore, it may be better not to use the method of "selecting by specifying the id with the verification tool" as much as possible. I made it like this because the test did not pass when I specified a string. If anyone is familiar with this, please let me know. Even if the error can be resolved, the part "why it is" has not been resolved, so I'm sorry for those who are reading this article. .. If you find out, please let me know: bow_tone1:

法人登録申請

Yes! That's why the administrator confirms the notification-the application approval test is over!

Finally, write a test to see if login restrictions are in place before and after approval. Those who have seen it so far will probably understand it, so please write it while referring to the code!

As a point, I will briefly explain only the description login_as (admin) in the before block in the corporate login test after approval! All I'm doing is simply logging in as an administrator, but the feature "login_as" doesn't have the "configure to enable devise helper methods in RSpec" I mentioned in the previous article. You can't use it, so be careful here! What is it? For those who thought, the previous article [1st] RSpec beginner wrote ModelSpec as a beginner that I wrote ModelSpec as a beginner.

At the end

This time, we mainly summarized the test of authentication of individual members and corporate members by system specifications. I was thinking of writing a test for posting, editing, DM, and notification of articles, but it was longer than I imagined, so I will divide it here. We will post the system specs of this function next time, so please take a look if you like.

Thank you for watching until the end!

** Added on 2020.10.01 ** As advised in the comments, we have corrected the parts such as fill_in. At that time, if the label tag is not written in the correct form, it will not work well, so be careful. Please refer to the comment section for how to write.

Reference article

[Introduction to RSpec that can be used, Part 4 "Any browser operation is free! Reverse Capybara Dictionary"](https://qiita.com/jnchito/items/607f956263c38a5fec24 "Introduction to RSpec that can be used, Part 4" Any browser operation is free ! Reverse Capybara Dictionary "") Be careful when creating with RSpec let ~ Lazy evaluation ~ When do you use RSpec let? (Translation)

Recommended Posts

[2nd] RSpec beginners wrote SystemSpec as a beginner (swamp edition)
[2nd] RSpec beginners wrote SystemSpec as a beginner (swamp edition)
[1st] RSpec beginners tried to write ModelSpec as a beginner
[1st] RSpec beginners tried to write ModelSpec as a beginner