Introduce RSpec and write unit test code

Overview

This time, I will summarize the flow of writing the unit test code of the user model by introducing rspec.

Along with that, FactoryBot and Faker were also introduced and implemented, so I will describe them.

What is Factory Bot?

It is a tool that creates data for testing.

What is Faker

You can randomly add content to the data created by FactoryBot.

Introduction and preparation of gem

As a point, pay attention to the location of the Gemfile. Not at the bottom.

gemfile


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

after this $ bundle install To do.

Next, execute the command to generate a directory etc. for writing RSpec.

Terminal


rails g rspec:install

This should generate directories and files.

Next, let's write the following in .rspec.

.rspec


--require spec_helper
--format documentation

Finally, let's generate a FactoryBot file. Create your own factories directory in the spec directory, and this time create users.rb.

Now the preparation is OK.

Specify a value in FactoryBot

Let's define each value in the file created earlier.

spec/factories/users.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}
    family_name { "Yamada" }
    first_name {"Taro"}
    read_family {"Yamada"}
    read_first {"Taro"}
    birth {Faker::Date.between(from: '1930-01-01', to: '2015-12-31')}
  end
end

The point here is that some use Faker and some do not. Faker can only randomly generate alphanumeric characters. Therefore, family_name etc. were priced appropriately this time.

There seems to be a gem that can create dummy data in Japanese, but this time I gave it up.

Writing test code

First of all, as a preparation, generate a file to write the test code with the command.

Terminal


rails g rspec:model user

Then spec/models/user_spec.rb Will be generated.

Let's write the unit test code there.

user_spec.rb


require 'rails_helper'

RSpec.describe User, type: :model do
  describe '#create' do
    before do
      @user = FactoryBot.build(:user)
    end

    it "Cannot register if nickname is empty" do
      @user.nickname = nil
      @user.valid?
      expect(@user.errors.full_messages).to include("Nickname can't be blank")
    end
    it "Cannot register if email is empty" do
      @user.email = nil
      @user.valid?
      expect(@user.errors.full_messages).to include("Email can't be blank")
    end
    it "You cannot register if the email is not unique" do
      @user.save
      another_user = FactoryBot.build(:user, email: @user.email)
      another_user.valid?
      expect(another_user.errors.full_messages).to include("Email has already been taken")
    end
---abridgement---
 end
end

Since it is long, I omitted it considerably, but I will describe it like this.

The point is Before do is to properly define FactoryBot data with instance variables. I forgot this and @user became nil and got lost once (de amateur)

If you can write it well, at the terminal

Terminal


bundle exec rspec spec/models/user_spec.rb

Enter the command and run the test.

Recommended Posts

Introduce RSpec and write unit test code
[RSpec] How to write test code
Let's introduce spock framework and write unit tests like test driven
RSpec test code execution
RSpec unit test code creation procedure (login user creation Ver.)
To implement, write the test and then code the process
[Rails] Test code using Rspec
Implementation of unit test code
Write test code in Spring Boot
[Rails5] Rspec -Unit test when nesting-
About app testing RSpec (unit test)
Copy and paste test with RSpec
[RSpec] Unit test (using gem: factory_bot)
Let's unit test with [rails] Rspec!
[RSpec on Rails] How to write test code for beginners by beginners
I tested how to use Ruby's test / unit and rock-paper-scissors code.
JUnit unit test pre-processing and post-processing order
[Rails] Unit test code for User model
I want to write a unit test!
Write code that is difficult to test
Write code using Ruby classes and instances
How to write an RSpec controller test
[Rspec] Flow from introducing Rspec to writing unit test code for a model
Articles and books useful for studying test code
After introducing RSpec, until you start writing unit test code for your model
Model unit test code to check uniqueness constraints
Efficient test code
How to write test code with Basic authentication
Unit test code for a model using RSpec, which has a little peculiarity ~ User registration
[Rails] From test preparation to model unit testing [RSpec]
How to write a unit test for Spring Boot 2
[Rails / RSpec] Write Model test (using Shoulda Matchers / FactoryBot)
Introduce JavaFX 15 and do GUI development with VS Code
WebAPI unit test and integration test with SpringBoot + Junit5, 4 patterns
How to dynamically write iterative test cases using test / unit (Test :: Unit)
Introduction to RSpec 1. Test, RSpec
Java Unit Test Library-Artery-Sample
[Test code learning / output]
[Rails] Test with RSpec
Test Nokogiri with Rspec.
Unit test with Junit.
I tried unit testing Rails app using RSpec and FactoryBot
I'm writing test code using RSpec, but it doesn't work