From f48a2990042f31d4dfc615a68b30f7b193373831 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 19 Feb 2026 06:11:46 -0500 Subject: [PATCH] Use validation matchers for `UrlValidator` spec (#37911) --- spec/validators/url_validator_spec.rb | 64 +++++++++------------------ 1 file changed, 22 insertions(+), 42 deletions(-) diff --git a/spec/validators/url_validator_spec.rb b/spec/validators/url_validator_spec.rb index 55c0347d18..56459b13fc 100644 --- a/spec/validators/url_validator_spec.rb +++ b/spec/validators/url_validator_spec.rb @@ -3,65 +3,45 @@ require 'rails_helper' RSpec.describe URLValidator do + subject { record_class.new } + let(:record_class) do Class.new do include ActiveModel::Validations + def self.name = 'Record' + attr_accessor :profile validates :profile, url: true end end - let(:record) { record_class.new } - describe '#validate_each' do - context 'with a nil value' do - it 'adds errors' do - record.profile = nil + context 'with a nil value' do + it { is_expected.to_not allow_value(nil).for(:profile).with_message(:invalid) } + end - expect(record).to_not be_valid - expect(record.errors.first.attribute).to eq(:profile) - expect(record.errors.first.type).to eq(:invalid) - end - end + context 'with an invalid url scheme' do + let(:invalid_scheme_url) { 'ftp://example.com/page' } - context 'with an invalid url scheme' do - it 'adds errors' do - record.profile = 'ftp://example.com/page' + it { is_expected.to_not allow_value(invalid_scheme_url).for(:profile).with_message(:invalid) } + end - expect(record).to_not be_valid - expect(record.errors.first.attribute).to eq(:profile) - expect(record.errors.first.type).to eq(:invalid) - end - end + context 'without a hostname' do + let(:no_hostname_url) { 'https:///page' } - context 'without a hostname' do - it 'adds errors' do - record.profile = 'https:///page' + it { is_expected.to_not allow_value(no_hostname_url).for(:profile).with_message(:invalid) } + end - expect(record).to_not be_valid - expect(record.errors.first.attribute).to eq(:profile) - expect(record.errors.first.type).to eq(:invalid) - end - end + context 'with an unparseable value' do + let(:non_numeric_port_url) { 'https://host:port/page' } - context 'with an unparseable value' do - it 'adds errors' do - record.profile = 'https://host:port/page' # non-numeric port string causes invalid uri error + it { is_expected.to_not allow_value(non_numeric_port_url).for(:profile).with_message(:invalid) } + end - expect(record).to_not be_valid - expect(record.errors.first.attribute).to eq(:profile) - expect(record.errors.first.type).to eq(:invalid) - end - end + context 'with a valid url' do + let(:valid_url) { 'https://example.com/page' } - context 'with a valid url' do - it 'does not add errors' do - record.profile = 'https://example.com/page' - - expect(record).to be_valid - expect(record.errors).to be_empty - end - end + it { is_expected.to allow_value(valid_url).for(:profile) } end end