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.
1 Installation of required gems 2 FactoryBot description 3 Writing test code
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.
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.
.rspec
--format documentation
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.
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.
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.
https://github.com/rspec/rspec-rails https://github.com/faker-ruby/faker
Recommended Posts