diff --git a/NEWS.md b/NEWS.md index 535d2b86..29f996ea 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # HEAD +* Fix `ensure_length_of` so that it uses the right message to validate when + `is_equal_to` is specified in conjunction with a custom message. + * Fix Rails/Test::Unit integration to ensure that the test case classes we are re-opening actually exist. diff --git a/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb b/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb index 7aa4f6c2..81717afb 100644 --- a/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb @@ -68,7 +68,6 @@ module Shoulda # :nodoc: end self end - alias_method :with_message, :with_short_message def with_long_message(message) if message @@ -77,6 +76,14 @@ module Shoulda # :nodoc: self end + def with_message(message) + if message + @short_message = message + @long_message = message + end + self + end + def description description = "ensure #{@attribute} has a length " if @options.key?(:minimum) && @options.key?(:maximum) diff --git a/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb b/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb index 36f26db7..ddcf036e 100644 --- a/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb +++ b/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb @@ -98,6 +98,13 @@ describe Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher do end end + context 'an attribute with a custom equal validation' do + it 'accepts ensuring the correct exact length' do + validating_length(:is => 4, :message => 'foobar'). + should ensure_length_of(:attr).is_equal_to(4).with_message(/foo/) + end + end + context 'an attribute without a length validation' do it 'rejects ensuring a minimum length' do define_model(:example, :attr => :string).new.