lib/matchers/validations/length_of.rb in mongoid-rspec-1.8.0 vs lib/matchers/validations/length_of.rb in mongoid-rspec-1.8.1

- old
+ new

@@ -1,9 +1,11 @@ module Mongoid module Matchers module Validations class ValidateLengthOfMatcher < HaveValidationMatcher + include WithMessage + def initialize(name) super(name, :length) end def with_maximum(value) @@ -28,27 +30,34 @@ @is = value self end alias :is :as_exactly + def with_message(message) + @expected_message = message + self + end + def matches?(actual) return false unless @result = super(actual) check_maximum if @maximum check_minimum if @minimum check_range if @within check_exact if @is + check_expected_message if @expected_message @result end def description options_desc = [] options_desc << "with minimum of #{@minimum}" if @minimum options_desc << "with maximum of #{@maximum}" if @maximum options_desc << "within the range of #{@within}" if @within options_desc << "as exactly #{@is}" if @is + options_desc << "with message '#{@expected_message}'" if @expected_message super << " #{options_desc.to_sentence}" end private @@ -98,9 +107,22 @@ def check_exact if actual_is == @is @positive_result_message << " as exactly #{@is}" else @negative_result_message << " as exactly #{actual_is}" + @result = false + end + end + + def check_expected_message + actual_message = @validator.options[:message] + if actual_message.nil? + @negative_result_message << " with no custom message" + @result = false + elsif actual_message == @expected_message + @positive_result_message << " with custom message '#{@expected_message}'" + else + @negative_result_message << " got message '#{actual_message}'" @result = false end end def actual_is