Add a with_message option to comparison matcher.
This commit is contained in:
parent
bcd8173383
commit
a8c6be7485
2
NEWS.md
2
NEWS.md
|
@ -1,4 +1,6 @@
|
|||
# HEAD
|
||||
* Fix a bug with the `validate_numericality_of` matcher that would not allow the
|
||||
`with_message` option on certain submatchers.
|
||||
|
||||
* Fix a regression with context-dependent validations in ActiveResource
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ module Shoulda # :nodoc:
|
|||
def initialize(value, operator)
|
||||
@value = value
|
||||
@operator = operator
|
||||
@message = nil
|
||||
end
|
||||
|
||||
def for(attribute)
|
||||
|
@ -18,13 +19,17 @@ module Shoulda # :nodoc:
|
|||
|
||||
def matches?(subject)
|
||||
@subject = subject
|
||||
disallows_value_of(value_to_compare)
|
||||
disallows_value_of(value_to_compare, @message)
|
||||
end
|
||||
|
||||
def allowed_types
|
||||
'integer'
|
||||
end
|
||||
|
||||
def with_message(message)
|
||||
@message = message
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def value_to_compare
|
||||
|
|
|
@ -26,6 +26,13 @@ describe Shoulda::Matchers::ActiveModel::ComparisonMatcher do
|
|||
it { instance_without_validations.should_not matcher.is_equal_to(0) }
|
||||
end
|
||||
|
||||
context 'with_message' do
|
||||
it 'verifies the message for the validation' do
|
||||
instance = instance_with_validations(:equal_to => 0, :message => 'Must be zero')
|
||||
instance.should matcher.is_equal_to(0).with_message('Must be zero')
|
||||
end
|
||||
end
|
||||
|
||||
def instance_with_validations(options = {})
|
||||
define_model :example, :attr => :string do
|
||||
validates_numericality_of :attr, options
|
||||
|
|
Loading…
Reference in New Issue