About method matchers used in model unit test code (RSpec)

Method list (partial)

①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 and hull_messages

** 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.

What is Matcha?

A method that compares the expected data with the actual data and returns the result ** matched ** or ** not matched **.

Matcha list (partial)

①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

About method matchers used in model unit test code (RSpec)
About app testing RSpec (unit test)
Password dummy data generation notes in Rails model unit test code
[Rails] Unit test code for User model
Introduce RSpec and write unit test code
[Rspec] Flow from introducing Rspec to writing unit test code for a model
Model unit test code to check uniqueness constraints
About regular expressions used in ruby sub method
After introducing RSpec, until you start writing unit test code for your model
RSpec test code execution
[Rails] From test preparation to model unit testing [RSpec]
RSpec unit test code creation procedure (login user creation Ver.)
[Rails / RSpec] Write Model test (using Shoulda Matchers / FactoryBot)
Unit test code for a model using RSpec, which has a little peculiarity ~ User registration
Code entry method in Qiita
Memorandum (task: model test code)
Java test code method collection
[Rails] Test code using Rspec
Implementation of unit test code
[Rails] About Rspec response test
Matcher often used in RSpec
About methods often used in devise
Write test code in Spring Boot
RSpec ~ Task model validation test creation
Test API often used in AssertJ
[Rails5] Rspec -Unit test when nesting-
[RSpec] How to write test code
[RSpec] Unit test (using gem: factory_bot)
Let's unit test with [rails] Rspec!
How to override in a model unit test so that Faker can be used to generate random values
I get got errors: User must exist in the unit model test
What is the model test code testing
[Ruby on Rails] Model test with RSpec