I'll leave a quick test code implementation here as a reminder of myself.
As a prerequisite, this time we will create a unit test code for the new registration of the user. There are four things to test: nickname, email, password, and password_confirmation.
gemfile
group :development, :test do
gem 'rspec-rails' #A gem for writing and testing these 4 lines
gem 'factory_bot_rails' #A gem that describes these 4 lines and creates a template-like one
gem 'faker' #A gem that describes these 4 lines and puts a text string etc.
gem 'pry-rails' #Describe these 4 lines, binding.A gem that allows pry to stop processing
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
After writing the above, execute the following in the terminal
Terminal
% bundle install
% rails g rspec:install
Perfect once these files are created
Terminal
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
Write the following in the .rspec file
.rspec
--require spec_helper #Included by default
--format documentation #Add this line
This description is for visualizing the result of the test code on the terminal.
Create a model with the following command (create a user model this time)
Terminal
% rails g rspec:model user
Once these are created
Terminal
create spec/models/user_spec.rb
invoke factory_bot
create spec/factories/users.rb
spec/models/user_spec.rb
require 'rails_helper'
RSpec.describe User, type: :model do
describe "New user registration" do
it "Cannot register if nickname is empty" do
#Write test code that cannot be registered if nickname is empty
end
it "Cannot register if email is empty" do
#Write a test code that cannot be registered if email is empty
end
it "Cannot register if password is empty" do
#Write a test code that cannot be registered if the password is empty
end
it "password_Cannot register if confirmation is empty" do
# password_Write a test code that cannot be registered if confirmation is empty
end
end
end
① describe "Content of the test you want to do" do ~ end Describe the content in "Content of the test you want to perform", this time describe the test of the user management function
② it'condition' do ~ end Describe the condition you want to test in the'condition'part
Execute the command to check if it is done properly
Terminal
% bundle exec rspec spec/models/user_spec.rb
If this is output to the terminal
First, create a directory called factories in the spec directory, and create user.rb in it.
Describe the following in the created users.rb
spec/factories/users.rb (when not using Faker)
FactoryBot.define do
factory :user do
nickname {"test"}
email {"test@example"}
password {"000000"}
password_confirmation {password}
end
end
spec/factories/users.rb (when using Faker)
FactoryBot.define do
factory :user do
nickname {Faker::Name.initials(number: 2)}
email {Faker::Internet.free_email}
password {Faker::Internet.password(min_length: 6)}
password_confirmation {password}
end
end
Now the factorybot is ready! ① Go back to the user_spec.rb file and add the following 3 lines under RSpec.describe User, type:: model do (2) Describe the actual processing to be performed in the processing of it do to end.
spec/models/user_spec.rb
require 'rails_helper'
RSpec.describe User, type: :model do
#⬇︎ Add these 3 lines
before do
@user = FactoryBot.build(:user)
end
#⬆︎ Add these 3 lines
describe "New user registration" do
it "Cannot register if nickname is empty" do
@user.nickname = ""
@user.valid?
expect(@user.errors.full_messages).to include("Nickname can't be blank")
end
it "Cannot register if email is empty" do
#⬇︎ Actual processing
@user.email = ""
@user.valid?
expect(@user.errors.full_messages).to include("Email can't be blank")
#⬆︎ Actual processing
end
it "Cannot register if password is empty" do
@user.password = ""
@user.valid?
expect(@user.errors.full_messages).to include("Password can't be blank")
end
it "password_Cannot register if confirmation is empty" do
@user.password_confirmation = ""
@user.valid?
expect(@user.errors.full_messages).to include("Password confirmation doesn't match Password")
end
end
end
Let's do it!
Terminal
% bundle exec rspec spec/models/user_spec.rb
When these are displayed, k If you get an error, write binding.pry in the error area and execute it!
spec/models/user_spec.rb
it "Cannot register if nickname is empty" do
@user.nickname = ""
@user.valid?
binding.pry #Processing stops here
expect(@user.errors.full_messages).to include("Nickname can't be blank")
end
Terminal
pry(#<RSpec::ExampleGroups::User>)> @user.valid? #Check if there is an error
pry(#<RSpec::ExampleGroups::User>)> @user.errors.full_messages #Check the error message
pry(#<RSpec::ExampleGroups::User>)> exit #Escape from the rails console
Recommended Posts