Add `failure_message_for_should_not` to `validate_numericality_of`

As well as to its submatchers:
* OnlyIntegerMatcher
* OddEvenNumberMatcher
This commit is contained in:
Melissa Xie 2013-04-12 13:11:07 -04:00
parent d680e7d47b
commit 3cfa565e52
5 changed files with 30 additions and 3 deletions

View File

@ -1,5 +1,8 @@
# HEAD
* Add missing `failure_message_for_should_not` implementations to
`validate_numericality_of` and its submatchers
* Support validation contexts for testing validations `on: :create` and when using custom contexts like `model.valid?(:my_context)`.
* Fix a bug in validations with autosaved models.

View File

@ -37,7 +37,11 @@ module Shoulda # :nodoc:
def failure_message_for_should
@disallow_value_matcher.failure_message_for_should
end
def failure_message_for_should_not
@disallow_value_matcher.failure_message_for_should_not
end
end
end
end
end
end

View File

@ -27,6 +27,10 @@ module Shoulda # :nodoc:
def failure_message_for_should
@disallow_value_matcher.failure_message_for_should
end
def failure_message_for_should_not
@disallow_value_matcher.failure_message_for_should_not
end
end
end
end

View File

@ -64,7 +64,11 @@ module Shoulda # :nodoc:
end
def failure_message_for_should
submatcher_failure_messages.last
submatcher_failure_messages_for_should.last
end
def failure_message_for_should_not
submatcher_failure_messages_for_should_not.last
end
private
@ -85,10 +89,14 @@ module Shoulda # :nodoc:
failing_submatchers.empty?
end
def submatcher_failure_messages
def submatcher_failure_messages_for_should
failing_submatchers.map(&:failure_message_for_should)
end
def submatcher_failure_messages_for_should_not
failing_submatchers.map(&:failure_message_for_should_not)
end
def failing_submatchers
@failing_submatchers ||= @submatchers.select { |matcher| !matcher.matches?(@subject) }
end

View File

@ -22,6 +22,14 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher do
define_model(:example, :attr => :string).new.should_not matcher
end
it 'rejects with the ActiveRecord :not_a_number message' do
the_matcher = matcher
the_matcher.matches?(define_model(:example, :attr => :string).new)
the_matcher.failure_message_for_should_not.should include 'Did not expect errors to include "is not a number"'
end
it 'rejects with the ActiveRecord :not_an_integer message' do
the_matcher = matcher.only_integer