mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
Fix checking case sensitivity when value is case insensitive
This commit is contained in:
parent
f67183ea59
commit
ada9bd3a1b
2 changed files with 49 additions and 0 deletions
|
@ -412,6 +412,14 @@ module Shoulda
|
||||||
if @options[:case_insensitive]
|
if @options[:case_insensitive]
|
||||||
disallows_value_of(swapcased_value, @expected_message)
|
disallows_value_of(swapcased_value, @expected_message)
|
||||||
else
|
else
|
||||||
|
if value == swapcased_value
|
||||||
|
raise NonCaseSwappableValueError.create(
|
||||||
|
model: @subject.class,
|
||||||
|
attribute: @attribute,
|
||||||
|
value: value
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
allows_value_of(swapcased_value, @expected_message)
|
allows_value_of(swapcased_value, @expected_message)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@ -551,6 +559,33 @@ module Shoulda
|
||||||
def column_for(scope)
|
def column_for(scope)
|
||||||
@subject.class.columns_hash[scope.to_s]
|
@subject.class.columns_hash[scope.to_s]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @private
|
||||||
|
class NonCaseSwappableValueError < Shoulda::Matchers::Error
|
||||||
|
attr_accessor :model, :attribute, :value
|
||||||
|
|
||||||
|
def message
|
||||||
|
Shoulda::Matchers.word_wrap <<-MESSAGE
|
||||||
|
Your #{model.name} model has a uniqueness validation on :#{attribute} which is
|
||||||
|
declared to be case-sensitive, but the value the uniqueness matcher used,
|
||||||
|
#{value.inspect}, doesn't contain any alpha characters, so using it to
|
||||||
|
to test the case-sensitivity part of the validation is ineffective. There are
|
||||||
|
two possible solutions for this depending on what you're trying to do here:
|
||||||
|
|
||||||
|
a) If you meant for the validation to be case-sensitive, then you need to give
|
||||||
|
the uniqueness matcher a saved instance of #{model.name} with a value for
|
||||||
|
:#{attribute} that contains alpha characters.
|
||||||
|
|
||||||
|
b) If you meant for the validation to be case-insensitive, then you need to
|
||||||
|
add `case_sensitive: false` to the validation and add `case_insensitive` to
|
||||||
|
the matcher.
|
||||||
|
|
||||||
|
For more information, please see:
|
||||||
|
|
||||||
|
http://matchers.shoulda.io/docs/v#{Shoulda::Matchers::VERSION}/file.NonCaseSwappableValueError.html
|
||||||
|
MESSAGE
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -450,6 +450,20 @@ describe Shoulda::Matchers::ActiveRecord::ValidateUniquenessOfMatcher, type: :mo
|
||||||
|
|
||||||
expect(record).to validate_uniqueness
|
expect(record).to validate_uniqueness
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'given an existing record where the value of the attribute under test is not case-swappable' do
|
||||||
|
it 'raises a NonCaseSwappableValueError' do
|
||||||
|
model = define_model_validating_uniqueness(
|
||||||
|
attribute_type: :string,
|
||||||
|
validation_options: { case_sensitive: true },
|
||||||
|
)
|
||||||
|
record = create_record_from(model, attribute_name => '123')
|
||||||
|
running_matcher = -> { validate_uniqueness.matches?(record) }
|
||||||
|
|
||||||
|
expect(&running_matcher).
|
||||||
|
to raise_error(described_class::NonCaseSwappableValueError)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the matcher is qualified with case_insensitive' do
|
context 'when the matcher is qualified with case_insensitive' do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue