1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
thoughtbot--shoulda-matchers/lib/shoulda/matchers/active_model/validation_message_finder.rb
Elliot Winkler a23081f759 Tweak allow_value failure_message
This commit changes the failure message that `allow_value` generates so
that it reads a bit nicer.

When a call to #valid? resulted in validation messages, `allow_value`
formerly failed with this message:

    Expected errors to include "the message" when attr is set to "some value", got errors: ["another message (attribute: \"attr\", value: \"some value\")", "some other message (attribute: \"attr2\", value: \"some other value\")"]

Now it fails with this message:

    Expected errors to include "the message" when attr is set to "some value",
    got errors:
    * "another message" (attribute: attr, value: "some value")
    * "some other message" (attribute: attr2, value: "some other value")

Similarly, when a call to #valid resulted in an exception, `allow_value`
formerly failed with this message:

    Expected errors to include "the message" when attr is set to "some value", got: some message

Now it fails with this message:

    Expected errors to include "the message" when attr is set to "some value",
    got: "some message"
2014-10-17 11:28:47 -06:00

69 lines
1.4 KiB
Ruby

module Shoulda
module Matchers
module ActiveModel
# @private
class ValidationMessageFinder
include Helpers
def initialize(instance, attribute, context=nil)
@instance = instance
@attribute = attribute
@context = context
end
def allow_description(allowed_values)
"allow #{@attribute} to be set to #{allowed_values}"
end
def expected_message_from(attribute_message)
attribute_message
end
def has_messages?
errors.present?
end
def source_description
'errors'
end
def messages_description
if errors.empty?
' no errors'
else
" errors:\n#{pretty_error_messages(validated_instance)}"
end
end
def messages
Array(messages_for_attribute)
end
private
def messages_for_attribute
if errors.respond_to?(:[])
errors[@attribute]
else
errors.on(@attribute)
end
end
def errors
validated_instance.errors
end
def validated_instance
@validated_instance ||= validate_instance
end
def validate_instance
@instance.valid?(*@context)
@instance
end
end
end
end
end