Use validation matchers for UrlValidator spec (#37911)

This commit is contained in:
Matt Jankowski 2026-02-19 06:11:46 -05:00 committed by GitHub
parent 093528ef17
commit f48a299004
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,65 +3,45 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe URLValidator do RSpec.describe URLValidator do
subject { record_class.new }
let(:record_class) do let(:record_class) do
Class.new do Class.new do
include ActiveModel::Validations include ActiveModel::Validations
def self.name = 'Record'
attr_accessor :profile attr_accessor :profile
validates :profile, url: true validates :profile, url: true
end end
end end
let(:record) { record_class.new }
describe '#validate_each' do context 'with a nil value' do
context 'with a nil value' do it { is_expected.to_not allow_value(nil).for(:profile).with_message(:invalid) }
it 'adds errors' do end
record.profile = nil
expect(record).to_not be_valid context 'with an invalid url scheme' do
expect(record.errors.first.attribute).to eq(:profile) let(:invalid_scheme_url) { 'ftp://example.com/page' }
expect(record.errors.first.type).to eq(:invalid)
end
end
context 'with an invalid url scheme' do it { is_expected.to_not allow_value(invalid_scheme_url).for(:profile).with_message(:invalid) }
it 'adds errors' do end
record.profile = 'ftp://example.com/page'
expect(record).to_not be_valid context 'without a hostname' do
expect(record.errors.first.attribute).to eq(:profile) let(:no_hostname_url) { 'https:///page' }
expect(record.errors.first.type).to eq(:invalid)
end
end
context 'without a hostname' do it { is_expected.to_not allow_value(no_hostname_url).for(:profile).with_message(:invalid) }
it 'adds errors' do end
record.profile = 'https:///page'
expect(record).to_not be_valid context 'with an unparseable value' do
expect(record.errors.first.attribute).to eq(:profile) let(:non_numeric_port_url) { 'https://host:port/page' }
expect(record.errors.first.type).to eq(:invalid)
end
end
context 'with an unparseable value' do it { is_expected.to_not allow_value(non_numeric_port_url).for(:profile).with_message(:invalid) }
it 'adds errors' do end
record.profile = 'https://host:port/page' # non-numeric port string causes invalid uri error
expect(record).to_not be_valid context 'with a valid url' do
expect(record.errors.first.attribute).to eq(:profile) let(:valid_url) { 'https://example.com/page' }
expect(record.errors.first.type).to eq(:invalid)
end
end
context 'with a valid url' do it { is_expected.to allow_value(valid_url).for(:profile) }
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
end end
end end