[RUBY] RSpec unit test code creation procedure (login user creation Ver.)

RSpec unit test code creation procedure (login user creation Ver.)

I thought it would be difficult to get used to it unless I summarized how to create RSpec unit test code so that I could understand it at a glance, so I tried to summarize the creation procedure myself.

1. Introducing a gem to use RSpec ・ Gemfile
group :development, :test do
  #abridgement
  gem 'rspec-rails'
end

·Terminal % bundle install

2. Introduced RSpec ·Terminal % rails g rspec:install

3. Settings for visualizing test code results on the terminal /.rspec
--require spec_helper
--format documentation

4. Introduced FactoryBot and Faker gems ・ Gemfile
group :development, :test do
  #abridgement
  gem 'rspec-rails'
  gem 'factory_bot_rails'
  gem 'faker'
end

·Terminal % bundle install

5. Prepare a dummy image public / images / dummy images

6. ~ Generate a file to write the test code of the model ·Terminal % rails g rspec: model model name

7. Use FactoryBot to set the value to instantiate the User model

(Example) 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)}
    #When using 6 characters or less ↑
    password_confirmation {password}
  end
end

8. Create an instance using FactoryBot

(Example) spec/models/user_spec.rb

require 'rails_helper'

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

9. Organize examples (Example sentence) ・ If nickname and email, password and password_confirmation exist, you can register. ・ If the nickname is empty, it cannot be registered. 〜 Such.

Write 10.example in the test code file

(Example) spec/models/user_spec.rb

describe 'New user registration' do
    it "nickname and email, password and password_You can register if confirmation exists" do
    end
・ ・ ・
end

11. Write and execute test code ·Terminal % bundle exec rspec spec/models/user_spec.rb

I hope I can make it to some extent by looking at this without memorizing it.

Recommended Posts

RSpec unit test code creation procedure (login user creation Ver.)
[Rails] Unit test code for User model
Introduce RSpec and write unit test code
RSpec test code execution
Unit test code for a model using RSpec, which has a little peculiarity ~ User registration
[Rails] Test code using Rspec
Implementation of unit test code
About method matchers used in model unit test code (RSpec)
RSpec ~ Task model validation test creation
[Rails5] Rspec -Unit test when nesting-
About app testing RSpec (unit test)
[RSpec] How to write test code
[RSpec] Unit test (using gem: factory_bot)
Let's unit test with [rails] Rspec!
[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
[For beginners] Test devise user registration with RSpec
Automatic creation of Java unit test result report
Model unit test code to check uniqueness constraints