[RAILS] How to override in a model unit test so that Faker can be used to generate random values

Introduction

I thought Faker could only be used with FactoryBot, but I realized that RSpec can also be used with files that describe unit test code for models, and I'll record what I've learned anew.

Faker

A description that randomly generates numbers.

As a basic usage,

Faker::Number.between(from: 5, to: 11) #Randomly generate 5 or more and 11 or less

You can also reduce the conditions.

Faker::Number.between(from: 11) #Randomly generate 11 or more

It says between, but if you don't addto:, you can generate a value without setting a maximum value. vice versa,

Faker::Number.between(to: 11) #Randomly generate 11 or less

If you change from: to to:, you can generate a value with only the highest value.

Specific usage

Purpose

It is a code to confirm whether 8 or more cannot be entered in the field for entering the grade.

Test code

before do
  @class_room = FactoryBot.build(:class_room)
end
it 'Cannot register if the grade is 8 or above' do
  @class_room.grade = Faker::Number.between(from: 8) #Overwrite the part you want to check
  @class_room.valid?
  expect(@class_room.errors.full_messages).to include("The grade is not on the list")
end

Finally

Since Rails makes it convenient to use Ruby, I just notice every day that there are many "why" black boxes. In this case, I assumed that Faker was only in FactoryBot.

Recommended Posts

How to override in a model unit test so that Faker can be used to generate random values
Object-oriented design that can be used when you want to return a response in form format
How to make a key pair of ecdsa in a format that can be read by Java
How to test a private method in Java and partially mock that method
[Android] I want to create a ViewPager that can be used for tutorials
[Ruby] How to generate a random alphabet string
How to automatically generate a constructor in Eclipse
Write a class that can be ordered in Java
[Ruby/Rails] How to generate a password in a regular expression
Convenient shortcut keys that can be used in Eclipse
About method matchers used in model unit test code (RSpec)
Create a page control that can be used with RecyclerView
Introduction to Rakefile that can be done in about 10 minutes
Find a Switch statement that can be converted to a Switch expression
Summary of ORM "uroboroSQL" that can be used in enterprise Java
How to implement a job that uses Java API in JobScheduler
I made a question that can be used for a technical interview
SwiftUI View that can be used in combination with other frameworks
How to rename a model with foreign key constraints in Rails
[Java 8] Until converting standard input that can be used in coding tests into a list or array
How to unit test Spring AOP
[Personal memo] How to interact with a random number generator in Java
Library summary that seems to be often used in recent Android development (2019/11)
A memo that enabled VS Code + JUnit 5 to be used on Windows 10
[Rspec] Flow from introducing Rspec to writing unit test code for a model