Skip to content

Commit fa4c9d5

Browse files
Improve validate_inclusion_of matcher boolean warn messages (#1692)
Closes #1688 The generated warn messages for the validate_inclusion_of matcher do not include the model/attribute names, which makes it hard to know where to look at to update/remove the tests. Following the library's existing warn messages behaviour of including the attribute in the message, I did the same here. I believe this is a simple but helpful improvement to help users fix those warnings. For example: https://github.com/thoughtbot/shoulda-matchers/blob/main/lib/shoulda/matchers/active_model/errors.rb#L17 I did consider adding the line/caller location, but I didn't find an example of that in the library, and decided to propose simply adding the model#attribute names.
1 parent c0b8352 commit fa4c9d5

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,6 @@ class ValidateInclusionOfMatcher < ValidationMatcher
278278
ARBITRARY_OUTSIDE_DATE = Date.jd(9999999)
279279
ARBITRARY_OUTSIDE_DATETIME = DateTime.jd(9999999)
280280
ARBITRARY_OUTSIDE_TIME = Time.at(9999999999)
281-
BOOLEAN_ALLOWS_BOOLEAN_MESSAGE = <<EOT.freeze
282-
You are using `validate_inclusion_of` to assert that a boolean column allows
283-
boolean values and disallows non-boolean ones. Be aware that it is not possible
284-
to fully test this, as boolean columns will automatically convert non-boolean
285-
values to boolean ones. Hence, you should consider removing this test.
286-
EOT
287-
BOOLEAN_ALLOWS_NIL_MESSAGE = <<EOT.freeze
288-
You are using `validate_inclusion_of` to assert that a boolean column allows nil.
289-
Be aware that it is not possible to fully test this, as anything other than
290-
true, false or nil will be converted to false. Hence, you should consider
291-
removing this test.
292-
EOT
293281

294282
def initialize(attribute)
295283
super(attribute)
@@ -400,6 +388,24 @@ def does_not_match?(subject)
400388

401389
private
402390

391+
def boolean_allows_boolean_message
392+
<<-EOT.strip
393+
You are using `validate_inclusion_of` to assert that the column '#{@subject.class.name}##{attribute}'
394+
allows boolean values and disallows non-boolean ones. Be aware that it
395+
is not possible to fully test this, as boolean columns will automatically
396+
convert non-boolean values to boolean ones. Hence, you should consider
397+
removing this test.
398+
EOT
399+
end
400+
401+
def boolean_allows_nil_message
402+
<<-EOT.strip
403+
You are using `validate_inclusion_of` to assert that the column '#{@subject.class.name}##{attribute}'
404+
allows nil. Be aware that it is not possible to fully test this, as anything other than
405+
true, false or nil will be converted to false. Hence, you should consider removing this test.
406+
EOT
407+
end
408+
403409
def minimum_range_value
404410
@range.begin
405411
end
@@ -492,11 +498,11 @@ def allows_any_value_outside_of_array?
492498
if attribute_type == :boolean
493499
case @array
494500
when [false, true], [true, false]
495-
Shoulda::Matchers.warn BOOLEAN_ALLOWS_BOOLEAN_MESSAGE
501+
Shoulda::Matchers.warn boolean_allows_boolean_message
496502
return true
497503
when [nil]
498504
if attribute_column.null
499-
Shoulda::Matchers.warn BOOLEAN_ALLOWS_NIL_MESSAGE
505+
Shoulda::Matchers.warn boolean_allows_nil_message
500506
return true
501507
else
502508
raise NonNullableBooleanError.create(@attribute)
@@ -513,11 +519,11 @@ def disallows_all_values_outside_of_array?
513519
if attribute_type == :boolean
514520
case @array
515521
when [false, true], [true, false]
516-
Shoulda::Matchers.warn BOOLEAN_ALLOWS_BOOLEAN_MESSAGE
522+
Shoulda::Matchers.warn boolean_allows_boolean_message
517523
return true
518524
when [nil]
519525
if attribute_column.null
520-
Shoulda::Matchers.warn BOOLEAN_ALLOWS_NIL_MESSAGE
526+
Shoulda::Matchers.warn boolean_allows_nil_message
521527
return true
522528
else
523529
raise NonNullableBooleanError.create(@attribute)

spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -690,12 +690,11 @@ def configure_validation_matcher(matcher)
690690
end
691691

692692
[[false, true], [true, false]].each do |booleans|
693-
it 'prints a warning' do
693+
it 'prints a warning with the model and attribute names' do
694694
valid_values = booleans
695695
builder = build_object_allowing(valid_values)
696696
message =
697-
'You are using `validate_inclusion_of` to assert that a boolean '\
698-
'column allows boolean values and disallows non-boolean ones'
697+
"You are using `validate_inclusion_of` to assert that the column 'Example#attr' allows boolean values and disallows non-boolean ones"
699698

700699
stderr = capture(:stderr) do
701700
expect_to_match_in_array(builder, valid_values)
@@ -750,12 +749,11 @@ def validation_matcher_scenario_args
750749
end
751750
end
752751

753-
it 'prints a warning' do
752+
it 'prints a warning with the model and attribute names' do
754753
valid_values = [nil]
755754
builder = build_object_allowing(valid_values)
756755
message =
757-
'You are using `validate_inclusion_of` to assert that a '\
758-
'boolean column allows nil'
756+
"You are using `validate_inclusion_of` to assert that the column 'Example#attr' allows nil"
759757

760758
stderr = capture(:stderr) do
761759
expect_to_match_in_array(builder, valid_values)

0 commit comments

Comments
 (0)