[Note] How to get started with Rspec

In making this article

A memorandum when developing an application with Ruby on Rails and then using it for another application (Mac as of October 21, 2020)

It took me a long time to get started, so please try it if you like to get a feel for it.

Gem used when doing Rspec

Gemfile


group :test do
  gem 'capybara', '>= 2.15'
  gem 'rspec-rails'
  gem "factory_bot_rails"
  gem 'faker'
end

Terminal


bundle install

rspec-rails Gem to run Rspec Ruby on Rails test framework RSpec getting started https://qiita.com/tatsurou313/items/c923338d2e3c07dfd9ee

capybara Rspec didn't work without driver settings, so I introduced Gem Capybara cheat sheet https://qiita.com/morrr/items/0e24251c049180218db4

faker Gem for creating data. Mainly the screen when executing Rspec is input test test data is randomly created using the library in Gem. Temporary data can also be entered in the seed file, which is convenient. I tried using Faker! (Usage and execution example) https://qiita.com/ginokinh/items/3f825326841dad6bd11e

factory_bot_rails I used it for debugging Gem, which can create test data first, mainly for Model. [Rails] Notes on how to use factory_bot https://qiita.com/at-946/items/aaff42e4a9c2e4dd58ed

Install Rspec

Terminal


rails generate rspec:install

The following files will be created when installed.

Terminal


  Running via Spring preloader in process 9045
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

Driver settings in spec_helper.rb

I don't understand how it works, and probably thanks to the settings here, I can run the program in the same environment as the browser.

spec_helper.rb


require 'capybara/rspec'
RSpec.configure do |config|
	config.before(:each, type: :system) do
    #driven_by :selenium_chrome_headless
    driven_by :rack_test
	end
end

After that, actually write a test program

You can create data to be created in the factories folder, model can test validation etc., and sysyem can test the actual operation on the screen. Code is shown for reference

factories/users_spec.rb


FactoryBot.define do
  factory :user do
    email { Faker::Internet.email }
    password { 'password' }
    password_confirmation { 'password' }
  end
end

model/user_spec.rb


require 'rails_helper'

RSpec.describe 'User model testing', type: :model do
  before do
    @user = build(:user)
  end

  describe 'Validation' do
    it 'NG if email is empty' do
      @user.email = ''
      expect(@user.valid?).to eq(false)
    end
  end
end

system/user_spec.rb


require 'rails_helper'

describe 'User authentication test' do
  describe 'New user registration' do
    before do
      visit new_user_registration_path
    end
    context 'Transition to new registration screen' do
      it 'Successful new registration' do
        fill_in 'user[email]', with: Faker::Internet.email
        fill_in 'user[password]', with: 'password'
        fill_in 'user[password_confirmation]', with: 'password'
        click_button "sign in"                       #The character displayed on the button
        expect(page).to have_content 'successful'   #Please specify the message that will be displayed after login
      end
    end
  end
end

Run Rspec

All the tests in the spec folder are executed by the following command, and the successful ones are displayed in green and the unsuccessful ones are displayed in red on the terminal.

Terminal


bundle exec rspec spec/ --format documentation

Thank you for your hard work.

Recommended Posts

[Note] How to get started with Rspec
How to get started with slim
How to get started with Eclipse Micro Profile
Rails beginners tried to get started with RSpec
How to get started with creating a Rails app
How to get along with Rails
How to get started with Gatsby (TypeScript) x Netlify x Docker
How to get started with JDBC using PostgresSQL on MacOS
I tried to get started with WebAssembly
Get started with Gradle
How to get resource files out with spring-boot
Get started with Spring boot
I tried to get started with Spring Data JPA
[Note] How to write Dockerfile/docker-compose.yml
How to number (number) with html.erb
Get started with DynamoDB with docker
How to update with activerecord-import
Get started with "Introduction to Practical Rust Programming" (Day 3)
Part2 Part II. How to proceed with Getting Started Spring Boot Reference Guide Note ①
How to get jdk etc from oracle with cli
How to get values in real time with TextWatcher (Android)
How to test a private method with RSpec for yourself
How to erase test image after running Rspec test with CarrierWave
Now is the time to get started with the Stream API
I tried to get started with Swagger using Spring Boot
How to scroll horizontally with ScrollView
Let's get started with parallel programming
[Rails] How to use devise (Note)
[Java] Points to note with Arrays.asList ()
How to enclose any character with "~"
How to get parameters in Spark
How to use mssql-tools with alpine
[RSpec] How to write test code
Memo to get with Struts2 + Ajax
How to start Camunda with Docker
[Rails / RSpec] How to deal with element has zero size error
Easy code review to get started with Jenkins / SonarQube: Static analysis
How to get boolean value with jQuery in rails simple form
How to install Adopt OpenJDK on Debian, Ubuntu with apt (-get)
[Note] How to restart the Windows container set up with docker-compose
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
How to share files with Docker Toolbox
How to get date data in Ruby
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
[Android] How to deal with dark themes
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to switch thumbnail images with JavaScript
[IOS] How to get data from DynamoDB
How to do API-based control with cancancan
[Java] How to get the current directory
How to achieve file download with Feign
How to update related models with accepts_nested_attributes_for
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to use Java Scanner class (Note)
How to get the date in java
How to handle sign-in errors with devise
[Note] How to use Rails 6 Devise + cancancan