Introducing Rspec, a Ruby on Rails test framework

What is a test?

It is difficult to manually check all the various behaviors of an application. Therefore, by writing a test, it is possible to efficiently check whether the data is passed correctly and whether it behaves as expected.

About test framework and Rspec in Ruby

The most famous Ruby test framework

Test type

There are several types of tests used in the Rails tutorial.

There is a unit test to check the operation of the model and view helper alone, a function test to check the call result of the controller / view, an integration test to check the behavior of the application across multiple controllers assuming the actual operation of the user, and the system In development, we often do the above tests.

Introduction

Put the gem for rspec in the Gemfile


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

after,

$ bundle install

Let.

If necessary, include FactoryBot (formerly FactoryGirl). References: https://qiita.com/at-946/items/aaff42e4a9c2e4dd58ed

grammar

Description of test requirements

describe , it , expect You can use these to describe your test requirements.

For example, in a unit test (model test),

spec/company_spec.rb



RSpec.describe Account, type: :model do
  describe 'company model' do
    it 'Create' do
      expect { create(:company) }.to change { Company.count }.by(1)
    end
  end
end

When an error occurs in Rspec, the text described in context, it is output. This will give you an idea of where the error occurred.

context You may branch the condition using context. You may create each of it For example, in the controller test

spec/company_spec.rb


RSpec.describe CompaniesController, type: :controller do
  describe 'Get list' do
    context 'If you are logged in' do
      it 'Returns 200' do
        //Test process
      end
    end
    context 'If you are not logged in' do
      it 'Returns a 400 error' do
        //Test process
      end
    end
  end
end

Use properly

Classification To describe
describe Subject to test
context Describe the case classification of specific conditions
it Describe what the output is

After describing the behavior as above, we will write the test.

let , let!

When using test data in the test, describe such processing. For example

spec/company_spec.rb


RSpec.describe CompaniesController, type: :controller do
  describe 'Get list' do
    context 'If you are logged in' do
      it 'Returns 200' do
        let(:company) { Company.create(name: 'Yamada Co., Ltd.', email: '[email protected]') }
        #Test process
      end
    end
    context 'If you are not logged in' do
      it 'Returns a 400 error' do
        #Test process
      end
    end
  end
end

Create data in the part where you write the process as described above.

let let! is each

Classification Evaluation timing
let Processed when the method is first called
let! Processed before block execution

In other words, in the above example

let(:company) { Company.new(name: 'Yamada Co., Ltd.', email: '[email protected]') }

At this point,: company has not been created and For example

process :show, method: :get, params: { company_id: company.id }

The difference is that it is created when company is called, as in.

An error may occur due to the difference in the timing of being called, so this difference is something I want to be aware of.

before / after If there is a process you want to perform before and after the test, describe the process here.

Data created with Rspec is basically deleted from the test database after testing.

At the end

I'm going to summarize what I personally mess with with Rspec. I would appreciate it if you could point out any mistakes!

References

https://qiita.com/jnchito/items/42193d066bd61c740612#context-%E3%81%A7%E6%9D%A1%E4%BB%B6%E5%88%A5%E3%81%AB%E3%82%B0%E3%83%AB%E3%83%BC%E3%83%97%E5%8C%96%E3%81%99%E3%82%8B https://qiita.com/uchiko/items/d34c5d1298934252f58f

Recommended Posts

Introducing Rspec, a Ruby on Rails test framework
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
Understand code coverage with Rspec, the Ruby on Rails test framework
Introducing Rspec with Ruby on Rails x Docker
[Ruby on Rails] Until the introduction of RSpec
Test run on rails
Ruby on Rails Elementary
[Ruby on Rails] A memorandum of layout templates
Ruby on Rails basics
[Rails] Test with RSpec
(Ruby on Rails6) Creating data in a table
Ruby On Rails Association
I made a portfolio with Ruby On Rails
[Ruby On Rails] Error in test using RSpec MySQL client is not connected
[Introduction] Try to create a Ruby on Rails application
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Build a Ruby on Rails development environment on AWS Cloud9
Run Ruby on Rails RSpec tests with GitHub Actions
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
[Rails] Test code using Rspec
Commentary on partial! --Ruby on Rails
Post a video on rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
[Rails] About Rspec response test
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[Ruby on Rails] I get a warning when running RSpec because gem'chromedriver-helper' is deprecated.
[Ruby on Rails] Add a column with a foreign key constraint
A note about the seed function of Ruby on Rails
How to display a graph in Ruby on Rails (LazyHighChart)
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Apply CSS to a specific View in Ruby on Rails
[Ruby on Rails] I get a warning when executing RSpec due to a different gem version.
The story of introducing a very Rails-like serverless framework "Ruby on Jets" into the production environment
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Rails5] Rspec -Unit test when nesting-
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict