This is an addition to the post the other day. Unit test code of model using RSpec which is a little peculiar ~ User registration
RSpec.describe School, type: :model do
describe 'School registration' do
before do
@school = FactoryBot.build(:school)
end
it 'The same school name cannot be registered' do
@school.save #← Point ①
another_school = FactoryBot.build(:school) #← Point ②
another_school.name = @school.name #← Point ③
another_school.valid?
expect(another_school.errors.full_messages).to include("The school name already exists")
end
end
Save FactoryBot.build (: school)
as @ school.save first.
Create another as another
(name is free).
Make the newly generated value at point ② and the previously saved value at point ① the same.
Recommended Posts