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.
It is a code to confirm whether 8 or more cannot be entered in the field for entering the grade.
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
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