[For Rails beginners] Summary of how to use RSpec (get an overview)

What is RSpec?

A gem that tests the operation of applications with RSpec and Ruby on Rails and Ruby.

As the purpose of use, check mistakes such as large-scale services are likely to occur, so code it. You can also save yourself the trouble of testing from scratch each time you update your application.

Introduction method

group :development, :test do
  gem "rspec-rails"
end

Make settings

% rails g rspec:install

Run this from your terminal to generate the required directories. Make sure you have a ** spec ** directory in your directory.

--format documentation

By writing this in .rspec, you can see the result of the test code from the terminal.

I will actually move it

This time, I will make a simple sample and explain it. First, create a sample directory under the spec directory generated by rails g rspec: install, and create a sample_spec.rb file in it. I will describe it there.

sample_spec.rb


RSpec.describe 'What test code?' do
  contenxt 'Situation explanation' do
    it "The result that this should be" do
Describe the processing content here
    end
  end
end

· RSpec.describe'What test code? 'do

After describe, describe the test contents in a large group. For example, *** New registration test *** and *** Login function test ***.

・ Context'Crush a little more and explain the situation' do

In the second line of describe, the test code declared in the first line is broken down a little more. For example, *** when new registration is possible *** or *** when login is not possible ***.

・ It "What kind of processing is it?" Do

Here, the specific processing content is described. For example, *** You can register newly if your name is entered *** or *** You cannot log in if your password is incorrect ***.

Explained using a concrete example

sample_spec.rb


RSpec.describe 'Test total score' do
  contenxt 'Total points when math is 100 points and national language is 50 points' do
    it "If you add the math and national language scores, you get a total of 150 points." do
      expect(100 + 50).to eq 150
    end
  end
end

Here is the test code for the total score of the test. In context, we explain the situation of how many points each score is. And in it, I explain specifically what should be like this and describe the processing contents in it.

Summary

The flow is to verbalize the confirmation content and execute the process. Next time, I would like to introduce what I learned about the processing contents.

Recommended Posts

[For Rails beginners] Summary of how to use RSpec (get an overview)
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Ruby] How to use slice for beginners
[Java] [Maven3] Summary of how to use Maven3
[RSpec on Rails] How to write test code for beginners by beginners
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
How to use an array for HashMap keys
(For beginners) [Rails] Time saving tech! How to install and use slim
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
Rails beginners tried to get started with RSpec
[For super beginners] How to use autofocus: true
How to implement login request processing (Rails / for beginners)
How to use an array for a TreeMap key
Rails / Ruby: How to get HTML text for Mail
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
[Rails] How to get the contents of strong parameters
How to use GitHub for super beginners (team development)
[Rails] How to use enum
How to use rails join
[Rails] How to use authenticate_user!
[Rails] How to use Scope
An unsupported Java version How to get rid of errors
Ruby on Rails for beginners! !! Summary of new posting functions
[Rails] How to display error messages for comment function (for beginners)
[Rails] How to use gem "devise"
How to use Java HttpClient (Get)
Summary of rails validation (for myself)
[Rails] How to use devise (Note)
[For beginners] Summary of java constructor
[Rails] How to use flash messages
How to use Ruby on Rails
How to use setDefaultCloseOperation () of JFrame
[Rails] How to use Active Storage
How to get along with Rails
[Introduction to Rails] How to use render
[Rails] How to get the URL of the transition source and redirect
[Swift5] How to get an array and the complement of arrays
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
How to get the contents of Map using for statement Memorandum
[For beginners] How to get the Ruby delayed railway line name
How to get the length of an audio file in java
How to use custom helpers in rails
[Ruby on Rails] How to use CarrierWave
[Rails] How to use rails console with docker
[java] Summary of how to handle char
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
Summary of how to write annotation arguments
[Note] How to get started with Rspec
How to use MySQL in Rails tutorial
Rails [For beginners] Implementation of comment function
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to write an RSpec controller test
How to use credentials.yml.enc introduced in Rails 5.2
[For beginners] How to debug in Eclipse