[RAILS] [Rspec] Flow from introducing Rspec to writing unit test code for a model

This time I will write down the procedure to introduce Rspec when writing test code instead of a memorandum.

Introducing Gem

Introduce 3 types of gems used in the test. Describe in the process of group: development,: test do ~ end.

Gemfile


group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rspec-rails
  gem 'factory_bot_rails'
  gem 'faker'
end

After writing, do bundle install and restart the local server.

Install the required files for Rspec

Terminal


% rails g rspec:install

Visualize test result logs

.rspec


--require spec_helper
--format documentation # =>Postscript

Set error message to English (optional)

Set if you want to display the test error message in English. RSpec.configure do |config|Added above.

spec/rails_helper.rb


#Omission
I18n.locale = "en"

RSpec.configure do |config|
#Omission

FactoryBot file creation

--Create a factories directory inside the spec directory. --And create a file in it to generate a test FactoryBot. --The name is model name s.rb. This time I want to write the test code of the user model, so I will use ʻusers.rb`.

Generate user information with FactoryBot

spec/factories/users.rb


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

Confirm that FactoryBot can be generated

Terminal (console)


pry(main)> FactoryBot.create(:user)

When an error occurs

--I got an error with the above command. At that time, it seems that the cause is that Spring is running behind the scenes, so use the spring stop command to stop Spring. --If you see Spring stopped., Spring has been stopped normally. --And then type FactoryBot.create (: user) again on the console to see the creation.

`Terminal``


#Exit the console with exit

% spring stop

Prepare a file to write the test code

--The following command will generate a file called spec / models / user_spec.rb.

Terminal


% rails g rspec:model user

Test code description

I will write test code more and more like this.

spec/models/user_spec.rb


require 'rails_helper'

RSpec.describe User, type: :model do
  pending "add some examples to (or delete) #{__FILE__}"# =>The original description is deleted

describe 'New user registration' do 
    before do
      @user = FactoryBot.build(:user)
    end

   it "name, email, password, password_If all confirmations are entered, it will be saved" do
      expect(@user).to be_valid
   end
   
  end
end

When running the test code

Run the following code.

Terminal


% bundle exec rspec spec/models/user_spec.rb

Reference site about how to write Faker

https://rubydoc.info/gems/faker/1.3.0/frames http://railscasts.com/episodes/126-populating-a-database https://github.com/takeyuweb/trygems/blob/master/try-faker/faker.md

Faker::Japanese https://github.com/tily/ruby-faker-japanese

Recommended Posts

[Rspec] Flow from introducing Rspec to writing unit test code for a model
After introducing RSpec, until you start writing unit test code for your model
[Rails] From test preparation to model unit testing [RSpec]
Unit test code for a model using RSpec, which has a little peculiarity ~ User registration
[Rails] Unit test code for User model
Model unit test code to check uniqueness constraints
How to write a unit test for Spring Boot 2
How to test a private method with RSpec for yourself
A memorandum for writing beautiful code
[RSpec] How to write test code
[RSpec on Rails] How to write test code for beginners by beginners
Sample code to unit test a Spring Boot controller with MockMvc
I want to write a unit test!
[Java] Flow from source code to execution
Introducing Rspec, a Ruby on Rails test framework
RSpec ~ Task model validation test creation
[Rails] Unit test code for User model
What is the model test code testing
Efficient test code
Model unit test code to check uniqueness constraints
RSpec test code execution
[Test code learning / output]
Java test code method collection
[Rails] Test code using Rspec
Implementation of unit test code
Password dummy data generation notes in Rails model unit test code
A memorandum for writing beautiful code
Write test code in Spring Boot
[RSpec] How to write test code
[Rspec] Flow from introducing Rspec to writing unit test code for a model
[RSpec] I wrote a test for uploading a profile image.
RSpec unit test code creation procedure (login user creation Ver.)
RSpec test code execution
Introduction to RSpec 1. Test, RSpec
What to do if you get a "302" error in your controller unit test code in Rails
I want to randomly generate information when writing test code
From studying programming for 2 months to releasing a web application
I'm writing test code using RSpec, but it doesn't work
[Integration test code] How to select an element from date_select
Introduction to RSpec 3. Model specs
Introduction to Micronaut 2 ~ Unit test ~
Implementation of unit test code
Only this I want to remember, 4 designs for writing unit tests
How to open a script file from Ubuntu with VS code
Let's write a test code for login function in Spring Boot
I tested how to use Ruby's test / unit and rock-paper-scissors code.
[Swift5] How to communicate from ViewController to Model and pass a value
Password dummy data generation notes in Rails model unit test code
How to override in a model unit test so that Faker can be used to generate random values
[I wanted to know this] Introducing RSpec with Docker + Rails-How to write a top page display confirmation test