Skip to content

Commit

Permalink
fix: confusing error message from validate_inclusion_of matcher (#1523)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsppedro committed Dec 16, 2022
1 parent 481712b commit da9b565
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

module Shoulda
module Matchers
class ExampleClass; end

module ActiveModel
# The `validate_inclusion_of` matcher tests usage of the
# `validates_inclusion_of` validation, asserting that an attribute can
Expand Down Expand Up @@ -269,7 +271,7 @@ def validate_inclusion_of(attr)
# @private
class ValidateInclusionOfMatcher < ValidationMatcher
BLANK_VALUES = ['', ' ', "\n", "\r", "\t", "\f"].freeze
ARBITRARY_OUTSIDE_STRING = 'shoulda-matchers test string'.freeze
ARBITRARY_OUTSIDE_STRING = Shoulda::Matchers::ExampleClass.name
ARBITRARY_OUTSIDE_INTEGER = 123456789
ARBITRARY_OUTSIDE_DECIMAL = BigDecimal('0.123456789')
ARBITRARY_OUTSIDE_DATE = Date.jd(9999999)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,41 @@ def define_simple_model(
def validation_matcher_scenario_args
super.deep_merge(model_creator: :active_record)
end

context 'against a polymorphic association' do
it 'matches when the subject configures validate_inclusion_of' do
define_model(:issue, severity_id: :integer, severity_type: :string) do
belongs_to :severity, polymorphic: true
validates_inclusion_of :severity_type, in: %w(Low Medium High)
end
define_model(:high)
define_model(:medium)
define_model(:low)

expect { validate_inclusion_of(:severity_type).in_array(%w(Low Medium High)) }.to match_against(Issue.new)
end

it 'does not match when subject does not set validate_inclusion_of' do
define_model(:issue, severity_id: :integer, severity_type: :string) do
belongs_to :severity, polymorphic: true
end
define_model(:high)
define_model(:medium)
define_model(:low)

expected_message = <<-MESSAGE.strip
Expected Issue to validate that :severity_type is either ‹"Low"›,
‹"Medium"›, or ‹"High"›, but this could not be proved.
After setting :severity_type to ‹"Shoulda::Matchers::ExampleClass"›,
the matcher expected the Issue to be invalid, but it was valid
instead.
MESSAGE

expect { validate_inclusion_of(:severity_type).in_array(%w(Low Medium High)) }.
not_to match_against(Issue.new).
and_fail_with(expected_message)
end
end
end

context 'for a plain Ruby attribute' do
Expand Down

0 comments on commit da9b565

Please sign in to comment.