From 3476ac5c2d732f1eb57ba638987c88f99af428b5 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Sun, 4 Jan 2026 13:10:08 +0100 Subject: [PATCH] Improve specs Use real attributes and match attribute name when getting client side hash. Since that attribute is only used to fetch a custom message, specs weren't failing before Additionally: - Do not test Rails edge against 3.2, because it now requires >= 3.3 - Do not test Ruby edge against Rails < 8.0 --- .github/workflows/ruby.yml | 9 --------- test/active_model/cases/test_absence_validator.rb | 4 ++-- .../cases/test_acceptance_validator.rb | 4 ++-- .../cases/test_confirmation_validator.rb | 10 +++++----- .../active_model/cases/test_exclusion_validator.rb | 10 +++++----- test/active_model/cases/test_format_validator.rb | 14 +++++++------- .../active_model/cases/test_inclusion_validator.rb | 10 +++++----- test/active_model/cases/test_length_validator.rb | 10 +++++----- test/active_model/cases/test_presence_validator.rb | 4 ++-- 9 files changed, 33 insertions(+), 42 deletions(-) diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 18e4df8d..12aff53a 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -20,9 +20,6 @@ jobs: channel: ['stable'] include: - - ruby-version: '3.2' - gemfile: rails_edge - channel: 'experimental' - ruby-version: '3.3' gemfile: rails_edge channel: 'experimental' @@ -32,12 +29,6 @@ jobs: - ruby-version: '4.0' gemfile: rails_edge channel: 'experimental' - - ruby-version: 'head' - gemfile: rails_7.1 - channel: 'experimental' - - ruby-version: 'head' - gemfile: rails_7.2 - channel: 'experimental' - ruby-version: 'head' gemfile: rails_8.0 channel: 'experimental' diff --git a/test/active_model/cases/test_absence_validator.rb b/test/active_model/cases/test_absence_validator.rb index d00d2999..474b3a05 100644 --- a/test/active_model/cases/test_absence_validator.rb +++ b/test/active_model/cases/test_absence_validator.rb @@ -7,13 +7,13 @@ class AbsenceValidatorTest < ClientSideValidations::ActiveModelTestBase def test_absence_client_side_hash expected_hash = { message: 'must be blank' } - assert_equal expected_hash, AbsenceValidator.new(attributes: [:name]).client_side_hash(@person, :age) + assert_equal expected_hash, AbsenceValidator.new(attributes: [:first_name]).client_side_hash(@person, :first_name) end def test_absence_client_side_hash_with_custom_message expected_hash = { message: 'is required to be blank' } - assert_equal expected_hash, AbsenceValidator.new(attributes: [:name], message: 'is required to be blank').client_side_hash(@person, :age) + assert_equal expected_hash, AbsenceValidator.new(attributes: [:first_name], message: 'is required to be blank').client_side_hash(@person, :first_name) end end end diff --git a/test/active_model/cases/test_acceptance_validator.rb b/test/active_model/cases/test_acceptance_validator.rb index 62d9faa8..e2494d54 100644 --- a/test/active_model/cases/test_acceptance_validator.rb +++ b/test/active_model/cases/test_acceptance_validator.rb @@ -7,13 +7,13 @@ class AcceptanceValidatorTest < ClientSideValidations::ActiveModelTestBase def test_acceptance_client_side_hash expected_hash = { message: 'must be accepted', accept: ['1', true] } - assert_equal expected_hash, AcceptanceValidator.new(attributes: [:name], class: Person).client_side_hash(@person, :age) + assert_equal expected_hash, AcceptanceValidator.new(attributes: [:first_name], class: Person).client_side_hash(@person, :first_name) end def test_acceptance_client_side_hash_with_custom_message expected_hash = { message: 'you must accept', accept: ['1', true] } - assert_equal expected_hash, AcceptanceValidator.new(attributes: [:name], class: Person, message: 'you must accept').client_side_hash(@person, :age) + assert_equal expected_hash, AcceptanceValidator.new(attributes: [:first_name], class: Person, message: 'you must accept').client_side_hash(@person, :first_name) end end end diff --git a/test/active_model/cases/test_confirmation_validator.rb b/test/active_model/cases/test_confirmation_validator.rb index 7e510792..6f6d6fc0 100644 --- a/test/active_model/cases/test_confirmation_validator.rb +++ b/test/active_model/cases/test_confirmation_validator.rb @@ -5,21 +5,21 @@ module ActiveModel class ConfirmationValidatorTest < ClientSideValidations::ActiveModelTestBase def test_confirmation_client_side_hash - expected_hash = { message: I18n.t('errors.messages.confirmation', attribute: 'Age'), case_sensitive: true } + expected_hash = { message: I18n.t('errors.messages.confirmation', attribute: 'First name'), case_sensitive: true } - assert_equal expected_hash, ConfirmationValidator.new(attributes: [:name], class: Person).client_side_hash(@person, :age) + assert_equal expected_hash, ConfirmationValidator.new(attributes: [:first_name], class: Person).client_side_hash(@person, :first_name) end def test_confirmation_client_side_without_case_sensitive - expected_hash = { message: I18n.t('errors.messages.confirmation', attribute: 'Age'), case_sensitive: false } + expected_hash = { message: I18n.t('errors.messages.confirmation', attribute: 'First name'), case_sensitive: false } - assert_equal expected_hash, ConfirmationValidator.new(attributes: [:name], class: Person, case_sensitive: false).client_side_hash(@person, :age) + assert_equal expected_hash, ConfirmationValidator.new(attributes: [:first_name], class: Person, case_sensitive: false).client_side_hash(@person, :first_name) end def test_confirmation_client_side_hash_with_custom_message expected_hash = { message: 'you must confirm', case_sensitive: true } - assert_equal expected_hash, ConfirmationValidator.new(attributes: [:name], class: Person, message: 'you must confirm').client_side_hash(@person, :age) + assert_equal expected_hash, ConfirmationValidator.new(attributes: [:first_name], class: Person, message: 'you must confirm').client_side_hash(@person, :first_name) end end end diff --git a/test/active_model/cases/test_exclusion_validator.rb b/test/active_model/cases/test_exclusion_validator.rb index 9bbed582..ff73a1f5 100644 --- a/test/active_model/cases/test_exclusion_validator.rb +++ b/test/active_model/cases/test_exclusion_validator.rb @@ -7,32 +7,32 @@ class ExclusionValidatorTest < ClientSideValidations::ActiveModelTestBase def test_exclusion_client_side_hash expected_hash = { message: 'is reserved', in: [1, 2] } - assert_equal expected_hash, ExclusionValidator.new(attributes: [:name], in: [1, 2]).client_side_hash(@person, :age) + assert_equal expected_hash, ExclusionValidator.new(attributes: [:first_name], in: [1, 2]).client_side_hash(@person, :first_name) end def test_exclusion_client_side_hash_with_custom_message expected_hash = { message: 'is exclusive', in: [1, 2] } - assert_equal expected_hash, ExclusionValidator.new(attributes: [:name], in: [1, 2], message: 'is exclusive').client_side_hash(@person, :age) + assert_equal expected_hash, ExclusionValidator.new(attributes: [:first_name], in: [1, 2], message: 'is exclusive').client_side_hash(@person, :first_name) end def test_exclusion_client_side_hash_with_ranges expected_hash = { message: 'is reserved', range: 1..2 } - assert_equal expected_hash, ExclusionValidator.new(attributes: [:name], in: 1..2).client_side_hash(@person, :age) + assert_equal expected_hash, ExclusionValidator.new(attributes: [:first_name], in: 1..2).client_side_hash(@person, :first_name) end def test_exclusion_client_side_hash_ignore_proc @person.stubs(:range).returns([1, 2]) - assert_nil ExclusionValidator.new(attributes: [:name], in: proc { |o| o.range }).client_side_hash(@person, :age) + assert_nil ExclusionValidator.new(attributes: [:first_name], in: proc { |o| o.range }).client_side_hash(@person, :first_name) end def test_exclusion_client_side_hash_observe_proc @person.stubs(:range).returns([1, 2]) expected_hash = { message: 'is reserved', in: [1, 2] } - assert_equal expected_hash, ExclusionValidator.new(attributes: [:name], in: proc { |o| o.range }).client_side_hash(@person, :age, true) + assert_equal expected_hash, ExclusionValidator.new(attributes: [:first_name], in: proc { |o| o.range }).client_side_hash(@person, :first_name, true) end end end diff --git a/test/active_model/cases/test_format_validator.rb b/test/active_model/cases/test_format_validator.rb index 785086d5..88a1c3de 100644 --- a/test/active_model/cases/test_format_validator.rb +++ b/test/active_model/cases/test_format_validator.rb @@ -7,45 +7,45 @@ class FormatValidatorTest < ClientSideValidations::ActiveModelTestBase def test_format_client_side_hash expected_hash = { message: 'is invalid', with: /.+/ } - assert_equal expected_hash, FormatValidator.new(attributes: [:name], with: /.+/).client_side_hash(@person, :age) + assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], with: /.+/).client_side_hash(@person, :first_name) end def test_format_client_side_hash_without expected_hash = { message: 'is invalid', without: /.+/ } - assert_equal expected_hash, FormatValidator.new(attributes: [:name], without: /.+/).client_side_hash(@person, :age) + assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], without: /.+/).client_side_hash(@person, :first_name) end def test_format_client_side_hash_with_custom_message expected_hash = { message: 'is wrong format', with: /.+/ } - assert_equal expected_hash, FormatValidator.new(attributes: [:name], with: /.+/, message: 'is wrong format').client_side_hash(@person, :age) + assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], with: /.+/, message: 'is wrong format').client_side_hash(@person, :first_name) end def test_format_client_side_hash_ignore_proc @person.stubs(:matcher).returns(/.+/) - assert_nil FormatValidator.new(attributes: [:name], with: proc { |o| o.matcher }).client_side_hash(@person, :age) + assert_nil FormatValidator.new(attributes: [:first_name], with: proc { |o| o.matcher }).client_side_hash(@person, :first_name) end def test_format_client_side_hash_without_ignore_proc @person.stubs(:matcher).returns(/.+/) - assert_nil FormatValidator.new(attributes: [:name], without: proc { |o| o.matcher }).client_side_hash(@person, :age) + assert_nil FormatValidator.new(attributes: [:first_name], without: proc { |o| o.matcher }).client_side_hash(@person, :first_name) end def test_format_client_side_hash_observe_proc @person.stubs(:matcher).returns(/.+/) expected_hash = { message: 'is invalid', with: /.+/ } - assert_equal expected_hash, FormatValidator.new(attributes: [:name], with: proc { |o| o.matcher }).client_side_hash(@person, :age, true) + assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], with: proc { |o| o.matcher }).client_side_hash(@person, :first_name, true) end def test_format_client_side_hash_without_observe_proc @person.stubs(:matcher).returns(/.+/) expected_hash = { message: 'is invalid', without: /.+/ } - assert_equal expected_hash, FormatValidator.new(attributes: [:name], without: proc { |o| o.matcher }).client_side_hash(@person, :age, true) + assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], without: proc { |o| o.matcher }).client_side_hash(@person, :first_name, true) end end end diff --git a/test/active_model/cases/test_inclusion_validator.rb b/test/active_model/cases/test_inclusion_validator.rb index 26f80589..8f2079ae 100644 --- a/test/active_model/cases/test_inclusion_validator.rb +++ b/test/active_model/cases/test_inclusion_validator.rb @@ -7,32 +7,32 @@ class InclusionValidatorTest < ClientSideValidations::ActiveModelTestBase def test_inclusion_client_side_hash expected_hash = { message: 'is not included in the list', in: [1, 2] } - assert_equal expected_hash, InclusionValidator.new(attributes: [:name], in: [1, 2]).client_side_hash(@person, :age) + assert_equal expected_hash, InclusionValidator.new(attributes: [:first_name], in: [1, 2]).client_side_hash(@person, :first_name) end def test_inclusion_client_side_hash_with_custom_message expected_hash = { message: 'is not a choice', in: [1, 2] } - assert_equal expected_hash, InclusionValidator.new(attributes: [:name], in: [1, 2], message: 'is not a choice').client_side_hash(@person, :age) + assert_equal expected_hash, InclusionValidator.new(attributes: [:first_name], in: [1, 2], message: 'is not a choice').client_side_hash(@person, :first_name) end def test_inclusion_client_side_hash_with_range expected_hash = { message: 'is not included in the list', range: 1..2 } - assert_equal expected_hash, InclusionValidator.new(attributes: [:name], in: 1..2).client_side_hash(@person, :age) + assert_equal expected_hash, InclusionValidator.new(attributes: [:first_name], in: 1..2).client_side_hash(@person, :first_name) end def test_inclusion_client_side_hash_ignore_proc @person.stubs(:range).returns([1, 2]) - assert_nil InclusionValidator.new(attributes: [:name], in: proc { |o| o.range }).client_side_hash(@person, :age) + assert_nil InclusionValidator.new(attributes: [:first_name], in: proc { |o| o.range }).client_side_hash(@person, :first_name) end def test_inclusion_client_side_hash_observe_proc @person.stubs(:range).returns([1, 2]) expected_hash = { message: 'is not included in the list', in: [1, 2] } - assert_equal expected_hash, InclusionValidator.new(attributes: [:name], in: proc { |o| o.range }).client_side_hash(@person, :age, true) + assert_equal expected_hash, InclusionValidator.new(attributes: [:first_name], in: proc { |o| o.range }).client_side_hash(@person, :first_name, true) end end end diff --git a/test/active_model/cases/test_length_validator.rb b/test/active_model/cases/test_length_validator.rb index 0ef8a54b..65a78bd9 100644 --- a/test/active_model/cases/test_length_validator.rb +++ b/test/active_model/cases/test_length_validator.rb @@ -12,7 +12,7 @@ def test_length_client_side_hash is: 10 } - assert_equal expected_hash, LengthValidator.new(attributes: [:age], is: 10).client_side_hash(@person, :first_name) + assert_equal expected_hash, LengthValidator.new(attributes: [:age], is: 10).client_side_hash(@person, :age) end def test_length_client_side_hash_with_allow_nil @@ -29,7 +29,7 @@ def test_length_client_side_hash_with_custom_message is: 10 } - assert_equal expected_hash, LengthValidator.new(attributes: [:age], is: 10, wrong_length: 'is the wrong length (should be %{count} words)').client_side_hash(@person, :first_name) + assert_equal expected_hash, LengthValidator.new(attributes: [:age], is: 10, wrong_length: 'is the wrong length (should be %{count} words)').client_side_hash(@person, :age) end def test_length_client_side_hash_with_custom_general_message @@ -42,7 +42,7 @@ def test_length_client_side_hash_with_custom_general_message maximum: 10 } - assert_equal expected_hash, LengthValidator.new(attributes: [:age], minimum: 4, maximum: 10, message: 'is not the correct length', too_long: 'is way too long').client_side_hash(@person, :first_name) + assert_equal expected_hash, LengthValidator.new(attributes: [:age], minimum: 4, maximum: 10, message: 'is not the correct length', too_long: 'is way too long').client_side_hash(@person, :age) end def test_length_client_side_hash_with_minimum_and_maximum @@ -55,7 +55,7 @@ def test_length_client_side_hash_with_minimum_and_maximum maximum: 10 } - assert_equal expected_hash, LengthValidator.new(attributes: [:age], minimum: 5, maximum: 10).client_side_hash(@person, :first_name) + assert_equal expected_hash, LengthValidator.new(attributes: [:age], minimum: 5, maximum: 10).client_side_hash(@person, :age) end def test_length_client_side_hash_with_range @@ -68,7 +68,7 @@ def test_length_client_side_hash_with_range maximum: 10 } - assert_equal expected_hash, LengthValidator.new(attributes: [:age], within: 5..10).client_side_hash(@person, :first_name) + assert_equal expected_hash, LengthValidator.new(attributes: [:age], within: 5..10).client_side_hash(@person, :age) end end end diff --git a/test/active_model/cases/test_presence_validator.rb b/test/active_model/cases/test_presence_validator.rb index 953bbe3f..d149636d 100644 --- a/test/active_model/cases/test_presence_validator.rb +++ b/test/active_model/cases/test_presence_validator.rb @@ -7,13 +7,13 @@ class PresenceValidatorTest < ClientSideValidations::ActiveModelTestBase def test_presence_client_side_hash expected_hash = { message: I18n.t('errors.messages.blank') } - assert_equal expected_hash, PresenceValidator.new(attributes: [:name]).client_side_hash(@person, :age) + assert_equal expected_hash, PresenceValidator.new(attributes: [:first_name]).client_side_hash(@person, :first_name) end def test_presence_client_side_hash_with_custom_message expected_hash = { message: 'is required' } - assert_equal expected_hash, PresenceValidator.new(attributes: [:name], message: 'is required').client_side_hash(@person, :age) + assert_equal expected_hash, PresenceValidator.new(attributes: [:first_name], message: 'is required').client_side_hash(@person, :first_name) end end end