Skip to content

Commit f35f615

Browse files
authored
Improve specs (#998)
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
1 parent cd590ad commit f35f615

9 files changed

Lines changed: 33 additions & 42 deletions

.github/workflows/ruby.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ jobs:
2020
channel: ['stable']
2121

2222
include:
23-
- ruby-version: '3.2'
24-
gemfile: rails_edge
25-
channel: 'experimental'
2623
- ruby-version: '3.3'
2724
gemfile: rails_edge
2825
channel: 'experimental'
@@ -32,12 +29,6 @@ jobs:
3229
- ruby-version: '4.0'
3330
gemfile: rails_edge
3431
channel: 'experimental'
35-
- ruby-version: 'head'
36-
gemfile: rails_7.1
37-
channel: 'experimental'
38-
- ruby-version: 'head'
39-
gemfile: rails_7.2
40-
channel: 'experimental'
4132
- ruby-version: 'head'
4233
gemfile: rails_8.0
4334
channel: 'experimental'

test/active_model/cases/test_absence_validator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class AbsenceValidatorTest < ClientSideValidations::ActiveModelTestBase
77
def test_absence_client_side_hash
88
expected_hash = { message: 'must be blank' }
99

10-
assert_equal expected_hash, AbsenceValidator.new(attributes: [:name]).client_side_hash(@person, :age)
10+
assert_equal expected_hash, AbsenceValidator.new(attributes: [:first_name]).client_side_hash(@person, :first_name)
1111
end
1212

1313
def test_absence_client_side_hash_with_custom_message
1414
expected_hash = { message: 'is required to be blank' }
1515

16-
assert_equal expected_hash, AbsenceValidator.new(attributes: [:name], message: 'is required to be blank').client_side_hash(@person, :age)
16+
assert_equal expected_hash, AbsenceValidator.new(attributes: [:first_name], message: 'is required to be blank').client_side_hash(@person, :first_name)
1717
end
1818
end
1919
end

test/active_model/cases/test_acceptance_validator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class AcceptanceValidatorTest < ClientSideValidations::ActiveModelTestBase
77
def test_acceptance_client_side_hash
88
expected_hash = { message: 'must be accepted', accept: ['1', true] }
99

10-
assert_equal expected_hash, AcceptanceValidator.new(attributes: [:name], class: Person).client_side_hash(@person, :age)
10+
assert_equal expected_hash, AcceptanceValidator.new(attributes: [:first_name], class: Person).client_side_hash(@person, :first_name)
1111
end
1212

1313
def test_acceptance_client_side_hash_with_custom_message
1414
expected_hash = { message: 'you must accept', accept: ['1', true] }
1515

16-
assert_equal expected_hash, AcceptanceValidator.new(attributes: [:name], class: Person, message: 'you must accept').client_side_hash(@person, :age)
16+
assert_equal expected_hash, AcceptanceValidator.new(attributes: [:first_name], class: Person, message: 'you must accept').client_side_hash(@person, :first_name)
1717
end
1818
end
1919
end

test/active_model/cases/test_confirmation_validator.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
module ActiveModel
66
class ConfirmationValidatorTest < ClientSideValidations::ActiveModelTestBase
77
def test_confirmation_client_side_hash
8-
expected_hash = { message: I18n.t('errors.messages.confirmation', attribute: 'Age'), case_sensitive: true }
8+
expected_hash = { message: I18n.t('errors.messages.confirmation', attribute: 'First name'), case_sensitive: true }
99

10-
assert_equal expected_hash, ConfirmationValidator.new(attributes: [:name], class: Person).client_side_hash(@person, :age)
10+
assert_equal expected_hash, ConfirmationValidator.new(attributes: [:first_name], class: Person).client_side_hash(@person, :first_name)
1111
end
1212

1313
def test_confirmation_client_side_without_case_sensitive
14-
expected_hash = { message: I18n.t('errors.messages.confirmation', attribute: 'Age'), case_sensitive: false }
14+
expected_hash = { message: I18n.t('errors.messages.confirmation', attribute: 'First name'), case_sensitive: false }
1515

16-
assert_equal expected_hash, ConfirmationValidator.new(attributes: [:name], class: Person, case_sensitive: false).client_side_hash(@person, :age)
16+
assert_equal expected_hash, ConfirmationValidator.new(attributes: [:first_name], class: Person, case_sensitive: false).client_side_hash(@person, :first_name)
1717
end
1818

1919
def test_confirmation_client_side_hash_with_custom_message
2020
expected_hash = { message: 'you must confirm', case_sensitive: true }
2121

22-
assert_equal expected_hash, ConfirmationValidator.new(attributes: [:name], class: Person, message: 'you must confirm').client_side_hash(@person, :age)
22+
assert_equal expected_hash, ConfirmationValidator.new(attributes: [:first_name], class: Person, message: 'you must confirm').client_side_hash(@person, :first_name)
2323
end
2424
end
2525
end

test/active_model/cases/test_exclusion_validator.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@ class ExclusionValidatorTest < ClientSideValidations::ActiveModelTestBase
77
def test_exclusion_client_side_hash
88
expected_hash = { message: 'is reserved', in: [1, 2] }
99

10-
assert_equal expected_hash, ExclusionValidator.new(attributes: [:name], in: [1, 2]).client_side_hash(@person, :age)
10+
assert_equal expected_hash, ExclusionValidator.new(attributes: [:first_name], in: [1, 2]).client_side_hash(@person, :first_name)
1111
end
1212

1313
def test_exclusion_client_side_hash_with_custom_message
1414
expected_hash = { message: 'is exclusive', in: [1, 2] }
1515

16-
assert_equal expected_hash, ExclusionValidator.new(attributes: [:name], in: [1, 2], message: 'is exclusive').client_side_hash(@person, :age)
16+
assert_equal expected_hash, ExclusionValidator.new(attributes: [:first_name], in: [1, 2], message: 'is exclusive').client_side_hash(@person, :first_name)
1717
end
1818

1919
def test_exclusion_client_side_hash_with_ranges
2020
expected_hash = { message: 'is reserved', range: 1..2 }
2121

22-
assert_equal expected_hash, ExclusionValidator.new(attributes: [:name], in: 1..2).client_side_hash(@person, :age)
22+
assert_equal expected_hash, ExclusionValidator.new(attributes: [:first_name], in: 1..2).client_side_hash(@person, :first_name)
2323
end
2424

2525
def test_exclusion_client_side_hash_ignore_proc
2626
@person.stubs(:range).returns([1, 2])
2727

28-
assert_nil ExclusionValidator.new(attributes: [:name], in: proc { |o| o.range }).client_side_hash(@person, :age)
28+
assert_nil ExclusionValidator.new(attributes: [:first_name], in: proc { |o| o.range }).client_side_hash(@person, :first_name)
2929
end
3030

3131
def test_exclusion_client_side_hash_observe_proc
3232
@person.stubs(:range).returns([1, 2])
3333
expected_hash = { message: 'is reserved', in: [1, 2] }
3434

35-
assert_equal expected_hash, ExclusionValidator.new(attributes: [:name], in: proc { |o| o.range }).client_side_hash(@person, :age, true)
35+
assert_equal expected_hash, ExclusionValidator.new(attributes: [:first_name], in: proc { |o| o.range }).client_side_hash(@person, :first_name, true)
3636
end
3737
end
3838
end

test/active_model/cases/test_format_validator.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,45 @@ class FormatValidatorTest < ClientSideValidations::ActiveModelTestBase
77
def test_format_client_side_hash
88
expected_hash = { message: 'is invalid', with: /.+/ }
99

10-
assert_equal expected_hash, FormatValidator.new(attributes: [:name], with: /.+/).client_side_hash(@person, :age)
10+
assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], with: /.+/).client_side_hash(@person, :first_name)
1111
end
1212

1313
def test_format_client_side_hash_without
1414
expected_hash = { message: 'is invalid', without: /.+/ }
1515

16-
assert_equal expected_hash, FormatValidator.new(attributes: [:name], without: /.+/).client_side_hash(@person, :age)
16+
assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], without: /.+/).client_side_hash(@person, :first_name)
1717
end
1818

1919
def test_format_client_side_hash_with_custom_message
2020
expected_hash = { message: 'is wrong format', with: /.+/ }
2121

22-
assert_equal expected_hash, FormatValidator.new(attributes: [:name], with: /.+/, message: 'is wrong format').client_side_hash(@person, :age)
22+
assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], with: /.+/, message: 'is wrong format').client_side_hash(@person, :first_name)
2323
end
2424

2525
def test_format_client_side_hash_ignore_proc
2626
@person.stubs(:matcher).returns(/.+/)
2727

28-
assert_nil FormatValidator.new(attributes: [:name], with: proc { |o| o.matcher }).client_side_hash(@person, :age)
28+
assert_nil FormatValidator.new(attributes: [:first_name], with: proc { |o| o.matcher }).client_side_hash(@person, :first_name)
2929
end
3030

3131
def test_format_client_side_hash_without_ignore_proc
3232
@person.stubs(:matcher).returns(/.+/)
3333

34-
assert_nil FormatValidator.new(attributes: [:name], without: proc { |o| o.matcher }).client_side_hash(@person, :age)
34+
assert_nil FormatValidator.new(attributes: [:first_name], without: proc { |o| o.matcher }).client_side_hash(@person, :first_name)
3535
end
3636

3737
def test_format_client_side_hash_observe_proc
3838
@person.stubs(:matcher).returns(/.+/)
3939
expected_hash = { message: 'is invalid', with: /.+/ }
4040

41-
assert_equal expected_hash, FormatValidator.new(attributes: [:name], with: proc { |o| o.matcher }).client_side_hash(@person, :age, true)
41+
assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], with: proc { |o| o.matcher }).client_side_hash(@person, :first_name, true)
4242
end
4343

4444
def test_format_client_side_hash_without_observe_proc
4545
@person.stubs(:matcher).returns(/.+/)
4646
expected_hash = { message: 'is invalid', without: /.+/ }
4747

48-
assert_equal expected_hash, FormatValidator.new(attributes: [:name], without: proc { |o| o.matcher }).client_side_hash(@person, :age, true)
48+
assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], without: proc { |o| o.matcher }).client_side_hash(@person, :first_name, true)
4949
end
5050
end
5151
end

test/active_model/cases/test_inclusion_validator.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@ class InclusionValidatorTest < ClientSideValidations::ActiveModelTestBase
77
def test_inclusion_client_side_hash
88
expected_hash = { message: 'is not included in the list', in: [1, 2] }
99

10-
assert_equal expected_hash, InclusionValidator.new(attributes: [:name], in: [1, 2]).client_side_hash(@person, :age)
10+
assert_equal expected_hash, InclusionValidator.new(attributes: [:first_name], in: [1, 2]).client_side_hash(@person, :first_name)
1111
end
1212

1313
def test_inclusion_client_side_hash_with_custom_message
1414
expected_hash = { message: 'is not a choice', in: [1, 2] }
1515

16-
assert_equal expected_hash, InclusionValidator.new(attributes: [:name], in: [1, 2], message: 'is not a choice').client_side_hash(@person, :age)
16+
assert_equal expected_hash, InclusionValidator.new(attributes: [:first_name], in: [1, 2], message: 'is not a choice').client_side_hash(@person, :first_name)
1717
end
1818

1919
def test_inclusion_client_side_hash_with_range
2020
expected_hash = { message: 'is not included in the list', range: 1..2 }
2121

22-
assert_equal expected_hash, InclusionValidator.new(attributes: [:name], in: 1..2).client_side_hash(@person, :age)
22+
assert_equal expected_hash, InclusionValidator.new(attributes: [:first_name], in: 1..2).client_side_hash(@person, :first_name)
2323
end
2424

2525
def test_inclusion_client_side_hash_ignore_proc
2626
@person.stubs(:range).returns([1, 2])
2727

28-
assert_nil InclusionValidator.new(attributes: [:name], in: proc { |o| o.range }).client_side_hash(@person, :age)
28+
assert_nil InclusionValidator.new(attributes: [:first_name], in: proc { |o| o.range }).client_side_hash(@person, :first_name)
2929
end
3030

3131
def test_inclusion_client_side_hash_observe_proc
3232
@person.stubs(:range).returns([1, 2])
3333
expected_hash = { message: 'is not included in the list', in: [1, 2] }
3434

35-
assert_equal expected_hash, InclusionValidator.new(attributes: [:name], in: proc { |o| o.range }).client_side_hash(@person, :age, true)
35+
assert_equal expected_hash, InclusionValidator.new(attributes: [:first_name], in: proc { |o| o.range }).client_side_hash(@person, :first_name, true)
3636
end
3737
end
3838
end

test/active_model/cases/test_length_validator.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_length_client_side_hash
1212
is: 10
1313
}
1414

15-
assert_equal expected_hash, LengthValidator.new(attributes: [:age], is: 10).client_side_hash(@person, :first_name)
15+
assert_equal expected_hash, LengthValidator.new(attributes: [:age], is: 10).client_side_hash(@person, :age)
1616
end
1717

1818
def test_length_client_side_hash_with_allow_nil
@@ -29,7 +29,7 @@ def test_length_client_side_hash_with_custom_message
2929
is: 10
3030
}
3131

32-
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)
32+
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)
3333
end
3434

3535
def test_length_client_side_hash_with_custom_general_message
@@ -42,7 +42,7 @@ def test_length_client_side_hash_with_custom_general_message
4242
maximum: 10
4343
}
4444

45-
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)
45+
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)
4646
end
4747

4848
def test_length_client_side_hash_with_minimum_and_maximum
@@ -55,7 +55,7 @@ def test_length_client_side_hash_with_minimum_and_maximum
5555
maximum: 10
5656
}
5757

58-
assert_equal expected_hash, LengthValidator.new(attributes: [:age], minimum: 5, maximum: 10).client_side_hash(@person, :first_name)
58+
assert_equal expected_hash, LengthValidator.new(attributes: [:age], minimum: 5, maximum: 10).client_side_hash(@person, :age)
5959
end
6060

6161
def test_length_client_side_hash_with_range
@@ -68,7 +68,7 @@ def test_length_client_side_hash_with_range
6868
maximum: 10
6969
}
7070

71-
assert_equal expected_hash, LengthValidator.new(attributes: [:age], within: 5..10).client_side_hash(@person, :first_name)
71+
assert_equal expected_hash, LengthValidator.new(attributes: [:age], within: 5..10).client_side_hash(@person, :age)
7272
end
7373
end
7474
end

test/active_model/cases/test_presence_validator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class PresenceValidatorTest < ClientSideValidations::ActiveModelTestBase
77
def test_presence_client_side_hash
88
expected_hash = { message: I18n.t('errors.messages.blank') }
99

10-
assert_equal expected_hash, PresenceValidator.new(attributes: [:name]).client_side_hash(@person, :age)
10+
assert_equal expected_hash, PresenceValidator.new(attributes: [:first_name]).client_side_hash(@person, :first_name)
1111
end
1212

1313
def test_presence_client_side_hash_with_custom_message
1414
expected_hash = { message: 'is required' }
1515

16-
assert_equal expected_hash, PresenceValidator.new(attributes: [:name], message: 'is required').client_side_hash(@person, :age)
16+
assert_equal expected_hash, PresenceValidator.new(attributes: [:first_name], message: 'is required').client_side_hash(@person, :first_name)
1717
end
1818
end
1919
end

0 commit comments

Comments
 (0)