From 48c102aabe6def99e7a170a97fe6d897bdf26e9e Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 27 Jan 2026 10:36:55 -0500 Subject: [PATCH] Use validation matchers for `DateOfBirthValidator` spec (#37631) --- .../date_of_birth_validator_spec.rb | 50 +++++-------------- 1 file changed, 13 insertions(+), 37 deletions(-) diff --git a/spec/validators/date_of_birth_validator_spec.rb b/spec/validators/date_of_birth_validator_spec.rb index 33e69e811b..65b63db234 100644 --- a/spec/validators/date_of_birth_validator_spec.rb +++ b/spec/validators/date_of_birth_validator_spec.rb @@ -3,49 +3,25 @@ require 'rails_helper' RSpec.describe DateOfBirthValidator do - let(:record_class) do - Class.new do - include ActiveModel::Validations + subject { Fabricate.build :user } - attr_accessor :date_of_birth + before { Setting.min_age = 16 } - validates :date_of_birth, date_of_birth: true - end + context 'with an invalid date' do + let(:invalid_date) { '76.830.10' } + + it { is_expected.to_not allow_values(invalid_date).for(:date_of_birth) } end - let(:record) { record_class.new } + context 'with a date below the age limit' do + let(:too_young) { 13.years.ago } - before do - Setting.min_age = 16 + it { is_expected.to_not allow_values(too_young).for(:date_of_birth).with_message(:below_limit) } end - describe '#validate_each' do - context 'with an invalid date' do - it 'adds errors' do - record.date_of_birth = '76.830.10' - - expect(record).to_not be_valid - expect(record.errors.first.attribute).to eq(:date_of_birth) - expect(record.errors.first.type).to eq(:invalid) - end - end - - context 'with a date below age limit' do - it 'adds errors' do - record.date_of_birth = 13.years.ago.strftime('%d.%m.%Y') - - expect(record).to_not be_valid - expect(record.errors.first.attribute).to eq(:date_of_birth) - expect(record.errors.first.type).to eq(:below_limit) - end - end - - context 'with a date above age limit' do - it 'does not add errors' do - record.date_of_birth = 16.years.ago.strftime('%d.%m.%Y') - - expect(record).to be_valid - end - end + context 'with a date above the age limit' do + let(:old_enough) { 16.years.ago } + + it { is_expected.to allow_values(old_enough).for(:date_of_birth) } end end