[RUBY] [Rails] How to define macros in Rspec and standardize processing

Target

Make login processing common.

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Rspec-rails: 4.0.1 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Implementation

1. Create a support directory

$ mkdir support

2. Create and edit a file in the support directory

$ touch spec/support/login_macros.rb

login_macros.rb


module LoginMacros
  def login(user)
    fill_in 'mail address', with: user.email
    fill_in 'password', with: user.password
    click_button 'Login'
  end
end

3. Edit rails_helper.rb

rails_helper.rb


#Comment out line 23
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

RSpec.configure do |config|
  config.include LoginMacros #Postscript
end

[Explanation]

① Read the support directory.

Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

(2) Enable the module defined in 2 to be used.

config.include LoginMacros

4. Use the method

require 'rails_helper'

RSpec.describe 'Authentication test', type: :feature do
  let(:user) { create(:user) }
  subject { page }

  describe 'User authentication test' do
    context 'User login test' do
      it 'Being able to log in' do
        visit new_user_session_path
        login(user) #Use method
        is_expected.to have_content 'Log out'
      end
    end
  end
end

Recommended Posts

[Rails] How to define macros in Rspec and standardize processing
Use Extend (Concerns) in Rails to standardize Controller processing.
[Rails] How to write in Japanese
How to introduce jQuery in Rails 6
How to install Swiper in Rails
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
How to delete large amounts of data in Rails and concerns
How to implement search functionality in Rails
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
How to standardize header footer in Thymeleaf
How to use MySQL in Rails tutorial
[rails] How to configure routing in resources
How to implement ranking functionality in Rails
How to implement asynchronous processing in Outsystems
How to use credentials.yml.enc introduced in Rails 5.2
How to debug the processing in the Ruby on Rails model only on the console
[Rails] How to use select boxes in Ransack
How to use "sign_in" in integration test (RSpec)
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to use scope and pass processing (Jakarta)
How to use JQuery in js.erb of Rails6
How to build API with GraphQL and Rails
How to execute processing before and after docker-entrypoint.sh
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] How to get success and error messages
How to make a follow function in Rails
How to define multiple orm.xml in Spring4, JPA2.1
[Rails] How to use PostgreSQL in Vagrant environment
Rails scope anti-patterns and how to eliminate them
How to check Rails commands in the terminal
How to write Rails
How to uninstall Rails
How to make an image partially transparent in Processing
How to make batch processing with Rails + Heroku configuration
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
Change date and time to Japanese notation in Rails
[Ruby On Rails] How to reset DB in Heroku
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
I want to define a function in Rails Console
How to convert A to a and a to A using AND and OR in Java
How to handle TSV files and CSV files in Ruby
How to query Array in jsonb with Rails + postgres
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to edit and customize devise view and controller
[Rails] How to load JavaScript in a specific view
(Ruby on Rails6) How to create models and tables
How to launch Swagger UI and Swagger Editor in Docker
[Rails] How to display an image in the view
[Java] How to get the key and value stored in Map by iterative processing
How to implement a circular profile image in Rails using CarrierWave and R Magick
[rails] How to post images