[RUBY] Rspec Basics [Rails]

A memorandum of basic RSpec grammar

Introduction

The function of the test example sentence is roughly written so that it is easy to remember, so we do not guarantee that it is suitable for programming.

References Ruby on Rails5 quick learning practice guide that can be used in the field

Introduction to RSpec that can be used, part 1 https://qiita.com/jnchito/items/42193d066bd61c740612

Prepare in advance

Create scaffold appropriately with Rails generator

python


rails new plactice

python


rails db:create

python


rails g scaffold user name:string age:integer

python


rails db:migrate

python


rails s

This completes the creation of the template. Create user.rb in the Model directory as shown below

model/user.rb


class User < ApplicationRecord
    def initialize(name:, age:)
        @name = name
        @age = age
    end

    def alcol
        if @age < 20
            "Alcohol has been around since I was 20 years old"
        else
            "Sake moderately!"
        end
    end
end

Complete with RSpec related gems and settings.

Organize and classify test cases

describe Declare test grouping and describe what spec you are trying to test.

describe can be nested On the outermost side, describe the theme of the entire file, and the deeper the nest, the finer the theme.

python


RSpec.describe 'User' do
    describe '#alcol_age' do

    end

end

Add RSpec. To the outermost describe.

context

The function is the same as describe (alias), but the meaning used is different. It is used when you want to divide the test content conditions for each variation.

python


RSpec.describe 'User' do
    describe '#alcol_age' do
        context 'If the user is under 20'do
            
        end

        context 'If the user is over 20 years old'do
            
        end
    end
end

it Describe the expected behavior of the function as a sentence. Write a test statement here And it is summarized in a unit called example.

If all the paths in this it are passed, it means that the example (test) has passed.

python


RSpec.describe 'User' do
    describe '#alcol_age' do
        context 'If the user is under 20'do
            it 'Attention Comments are displayed for under 20 years old' do
                user = User.new(name: 'A', age: 18)
                expect(user.alcol_age).to have_content 'Alcohol has been around since I was 20 years old'
            end
        end

        context 'If the user is over 20 years old'do
            it 'Note The comment must be displayed for people over 20 years old.' do
                user = User.new(name: 'A', age: 20)
                expect(user.alcol_age).to have_content 'Sake moderately!'
            end
        end
    end
end

before If before is described in describe or context, the code written in the before block is executed before the test code in that area is executed. I used to create a user with it, but writing before is easier to read and can be used under multiple conditions, so the effect of making it DRY can also be obtained.

python


RSpec.describe 'User' do
    describe '#alcol_age' do
        context 'If the user is under 20'do
            it 'Attention Comments are displayed for under 20 years old' do
                user = User.new(name: 'A', age: 18)
                expect(user.alcol_age).to have_content 'Alcohol has been around since I was 20 years old'
            end
        end

        context 'If the user is over 20 years old'do
            it 'Note The comment must be displayed for people over 20 years old.' do
                user = User.new(name: 'B', age: 20)
                expect(user.alcol_age).to have_content 'Sake moderately!'
            end
        end
    end
end

before is executed every time it is executed. The state of the database will be restored by the time the next it is executed, so it is basically not that another test case is affected by the test case.

python


RSpec.describe 'User' do
    describe '#alcol_age' do
        before do
            @params = {name: 'A', age: 18}
        end

        context 'If the user is under 20'do
            before do
                @params.merge!(age: 18)
            end
            it 'Attention Comments are displayed for under 20 years old' do
                user = User.new(@params)
                expect(user.alcol_age).to have_content 'Alcohol has been around since I was 20 years old'
            end
        end

        context 'If the user is over 20 years old'do
            before do
                @params.merge!(age: 20)
            end
            it 'Note The comment must be displayed for people over 20 years old.' do
                user = User.new(@params)
                expect(user.alcol_age).to have_content 'Sake moderately!'
            end
        end
    end
end

let Variable-like things that can be used in tests. Not a variable, but the behavior used is close to a variable

python


let (Definition name) {Definition content}

let is called the first time let is called. (Lazy evaluation) So be careful as it may never be called.

python


RSpec.describe 'User' do
    describe '#alcol_age' do
        let(:user) {User.new(params)}
        let(:params) {{name: 'A', age: age}}

        context 'If the user is under 20'do
            let(:age) {18}
            it 'Attention Comments are displayed for under 20 years old' do
                expect(user.alcol_age).to have_content 'Alcohol has been around since I was 20 years old'
            end
        end

        context 'If the user is over 20 years old'do
            let(:age) {20}
            it 'Note The comment must be displayed for people over 20 years old.' do
                expect(user.alcol_age).to have_content 'Sake moderately!'
            end
        end
    end
end

That's all for the basics Other grammars will be added separately.

If you have any questions, I would be grateful if you could ask for a professor.

Recommended Posts

Rspec Basics [Rails]
Rails basics
RSpec Basics
Rails CSV basics
Rails Routing Basics
Rails database basics
[Rails5] Rspec -validation-
About RSpec (Rails)
Ruby on Rails basics
[Rails] Test with RSpec
Rails, RSpec installation procedure
[Rails] Introduction of devise Basics
[RSpec] Let's use FactoryBot [Rails]
[Rails] Test code using Rspec
[Rails] About Rspec response test
Rails book review app RSpec introduction
[Rails5] Rspec -Unit test when nesting-
[Rails] RSpec Kihon no Ki [Introduction]
Let's unit test with [rails] Rspec!
[Rails g.error]
Rails Review 1
RSpec setup
Ruby basics
Ruby basics
[Rails] first_or_initialize
[Ruby on Rails] View test with RSpec
Introducing RSpec
rails tutorial
Fragment basics
JPA Basics 1
About Rails 6
Docker basics
ViewPager basics
Rails foundation
rails tutorial
RSpec preparation
Java basics
rails tutorial
Install RSpec
rails tutorial
Java basics
RSpec Setup
Rspec, TDD (1)
[Rails] devise
[Ruby on Rails] Model test with RSpec
rails tutorial
Hello RSpec
JPA Basics 2
rails tutorial
Hash basics
Java basics
Rails Tips
rails method
rails tutorial
Ruby basics
[Rails] ActiveRecord
[Rails] form_with
RecyclerView Basics
Rails Review 2
I rewrote the Rails tutorial test with RSpec
[Ruby on Rails] Until the introduction of RSpec