Use validation matchers for `DateOfBirthValidator` spec (#37631)

pull/34053/merge
Matt Jankowski 4 weeks ago committed by GitHub
parent 34f0482ad4
commit 48c102aabe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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

Loading…
Cancel
Save