[RUBY] [Rails] Unit test code for User model

Introduction

I implemented the test code using a gem called Rspec. This time, we confirmed the behavior of the normal system and the abnormal system for the unit test code of the user registration function.

table of contents

1 Installation of required gems 2 FactoryBot description 3 Writing test code

1. Installation of required gems

Add the following in ** group: development,: test ** of the gemfile. Then run bundle install.

gemfile


gem 'pry-rails'
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'faker'

Next, create a directory to write Rspec.

Terminal
rails g rpsec:install

When executed, the directories and files will be generated as shown below.

Terminal
create  .rspec
create  spec
create  spec/spec_helper.rb
create  spec/rails_helper.rb

Describe to check the result of the test code on the terminal.

.rspec


--format documentation

2. FactoryBot description

FactoryBot is a gem that can hold instances together. Create a factories directory in the spec directory and create a user.rb file in it. Edit user.rb as follows.

spec/factories/user.rb


FactoryBot.define do
  factory :user do
    nickname { Faker::Name.last_name }
    email { Faker::Internet.free_email }
    password = Faker::Internet.password(min_length: 6)
    password { password }
    password_confirmation { password }
  end
end

Faker is a gem that generates random values.

3. Writing test code

Edit spec / models / user_spec.rb as follows.

spec/models/user_spec.rb


require 'rails_helper'

RSpec.describe User, type: :model do
  describe User do
    before do
      @user = FactoryBot.build(:user)
    end
    describe 'New user registration' do
      context 'When new registration is successful' do
        it 'nickname and email, password and password_Being able to register if confirmation exists' do
          expect(@user).to be_valid
        end
        it 'You can register if your password is 6 characters or more' do
          @user.password = '123456'
          @user.password_confirmation = '123456'
          expect(@user).to be_valid
        end
      end

      context 'When new registration does not go well' do
        it 'Cannot register if nickname is empty' do
          @user.nickname = nil
          @user.valid?
          expect(@user.errors.full_messages).to include('Please enter a nickname')
        end
        it 'Cannot register if email is empty' do
          @user.email = nil
          @user.valid?
          expect(@user.errors.full_messages).to include('Please enter your email')
        end
        it 'Cannot register if there are duplicate emails' do
          @user.save
          another_user = FactoryBot.build(:user, email: @user.email)
          another_user.valid?
          expect(another_user.errors.full_messages).to include('Email already exists')
        end
        it 'Cannot register if password is empty' do
          @user.password = nil
          @user.valid?
          expect(@user.errors.full_messages).to include('Please enter your password')
        end
        it 'password even if password exists_Confirmation cannot be registered if it is empty' do
          @user.password_confirmation = ''
          @user.valid?
          expect(@user.errors.full_messages).to include('Password (for confirmation) and password input do not match')
        end
        it 'Cannot register if password is 5 characters or less' do
          @user.password = '12345'
          @user.password_confirmation = '12345'
          @user.valid?
          expect(@user.errors.full_messages).to include('Please enter the password with at least 6 characters')
        end
      end
    end
  end
end

Run the test code with the following command.

Terminal
bundle exec rspec spec/models/user_spec.rb 

This time, since'rails-i18n'is used, the error message is written in Japanese. By using a gem called pry-rails, the execution of the test code is stopped halfway and the error message is confirmed.

Reference link

https://github.com/rspec/rspec-rails https://github.com/faker-ruby/faker

Recommended Posts

[Rails] Unit test code for User model
Unit test code for a model using RSpec, which has a little peculiarity ~ User registration
Password dummy data generation notes in Rails model unit test code
Model unit test code to check uniqueness constraints
[Rspec] Flow from introducing Rspec to writing unit test code for a model
[Rails] From test preparation to model unit testing [RSpec]
After introducing RSpec, until you start writing unit test code for your model
RSpec unit test code creation procedure (login user creation Ver.)
Memorandum (task: model test code)
[Rails] Test code using Rspec
Implementation of unit test code
About method matchers used in model unit test code (RSpec)
[Rails5] Rspec -Unit test when nesting-
Let's unit test with [rails] Rspec!
[RSpec on Rails] How to write test code for beginners by beginners
Introduce RSpec and write unit test code
What is the model test code testing
[Ruby on Rails] Model test with RSpec
I get got errors: User must exist in the unit model test
Articles and books useful for studying test code
Use [Rails] devise Guest user function (for portfolio)
Rails tutorial test
[For beginners] Test devise user registration with RSpec
[Rails] How to implement unit tests for models
Efficient test code
[Rails] Model Association (Association)
How to write a unit test for Spring Boot 2
[Rails / RSpec] Write Model test (using Shoulda Matchers / FactoryBot)
Rails5 Rspec test error ArgumentError: Factory not registered: user
[Rails Tutorial] "NoMethodError (undefined method` activation_digest ='for # <User: 0x00000003156938>
Model association in Rails
Test run on rails
Java Unit Test Library-Artery-Sample
[Test code learning / output]
[Rails] Test with RSpec
What to do if you get a "302" error in your controller unit test code in Rails
Unit test with Junit.
Definitely useful! Debug code for development in Ruby on Rails
Rails: I've summarized the model and database for a moment.
Use Spring Test + Mockito + JUnit 4 for Spring Boot + Spring Retry unit tests
Build a bulletin board API with authentication authorization in Rails 6 # 11 User model test and validation added