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


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.


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

Next, create a directory to write Rspec.

rails g rpsec:install

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

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.


--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.


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 }

Faker is a gem that generates random values.

3. Writing test code

Edit spec / models / user_spec.rb as follows.


require 'rails_helper'

RSpec.describe User, type: :model do
  describe User do
    before do
      @user = FactoryBot.build(:user)
    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
        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

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

Run the test code with the following command.

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