From 3cfa565e52b97c3514a2e690e540316c822a308f Mon Sep 17 00:00:00 2001 From: Melissa Xie Date: Fri, 12 Apr 2013 13:11:07 -0400 Subject: [PATCH] Add `failure_message_for_should_not` to `validate_numericality_of` As well as to its submatchers: * OnlyIntegerMatcher * OddEvenNumberMatcher --- NEWS.md | 3 +++ .../matchers/active_model/odd_even_number_matcher.rb | 6 +++++- .../matchers/active_model/only_integer_matcher.rb | 4 ++++ .../active_model/validate_numericality_of_matcher.rb | 12 ++++++++++-- .../validate_numericality_of_matcher_spec.rb | 8 ++++++++ 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8f136896..bdeec843 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/lib/shoulda/matchers/active_model/odd_even_number_matcher.rb b/lib/shoulda/matchers/active_model/odd_even_number_matcher.rb index c84dcdea..c152317d 100644 --- a/lib/shoulda/matchers/active_model/odd_even_number_matcher.rb +++ b/lib/shoulda/matchers/active_model/odd_even_number_matcher.rb @@ -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 \ No newline at end of file +end diff --git a/lib/shoulda/matchers/active_model/only_integer_matcher.rb b/lib/shoulda/matchers/active_model/only_integer_matcher.rb index dff2c922..c36faf2c 100644 --- a/lib/shoulda/matchers/active_model/only_integer_matcher.rb +++ b/lib/shoulda/matchers/active_model/only_integer_matcher.rb @@ -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 diff --git a/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb index 743eb648..4c1ec318 100644 --- a/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb @@ -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 diff --git a/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb b/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb index a1da0412..df39afc4 100644 --- a/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +++ b/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb @@ -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