①valid? A method that means "correct" in Japanese and determines "whether it is correct". Basically used for abnormal system testing. If the created data is saved correctly, ** true ** is returned, and if it is not saved, ** false ** is returned. If it is not saved, it also generates a ** error message ** indicating "why not saved".
users.rb
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
user_spec.rb
require 'rails_helper'
describe User, type: :model do
before do
@user = FactoryBot.build(:user)
end
describe 'New user registration' do
context 'When new registration is successful' do
#(abridgement)
end
context 'When new registration does not go well' do
it "Cannot register if the nickname is empty" do
@user.nickname = ''
@user.valid? # <=this part
# => false
end
end
end
end
** errors ** is a method that displays the errors of the data judged by ** valid? **. ** hull_messages ** is a method for outputting error messages. Basically, these two methods are used in combination.
users.rb
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
user_spec.rb
require 'rails_helper'
describe User, type: :model do
before do
@user = FactoryBot.build(:user)
end
describe 'New user registration' do
context 'When new registration is successful' do
#(abridgement)
end
context 'When new registration does not go well' do
it "Cannot register if the nickname is empty" do
@user.nickname = ''
@user.valid?
expect(@user.errors.full_messages).to include("Please enter a nickname") # <=this part
end
end
end
end
After this, I will introduce it again, but ** include ** is a matcher that has the meaning of "include" in Japanese and can confirm "whether the character string Y is included in X". is.
A method that compares the expected data with the actual data and returns the result ** matched ** or ** not matched **.
①include ** include ** is a matcher that has the meaning of "include" in Japanese and allows you to check "whether or not the character string Y is included in X".
users.rb
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
user_spec.rb
require 'rails_helper'
describe User, type: :model do
before do
@user = FactoryBot.build(:user)
end
describe 'New user registration' do
context 'When new registration is successful' do
#(abridgement)
end
context 'When new registration does not go well' do
it "Cannot register if the nickname is empty" do
@user.nickname = ''
@user.valid?
expect(@user.errors.full_messages).to include("Please enter a nickname") # <=this part
# < X > < Y >
end
end
end
②be_valid ** be_valid ** is a method that determines that an instance of expect is saved correctly. Basically used in normal testing.
users.rb
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
user_spec.rb
describe 'New user registration' do
context 'When new registration is successful' do
it "You can register if you have a nickname, email address, password, and password (for confirmation)." do
expect(@user).to be_valid
end
end
context 'When new registration does not go well' do
#(abridgement)
end
end
Recommended Posts