mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
a23081f759
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"
58 lines
1.2 KiB
Ruby
58 lines
1.2 KiB
Ruby
module Shoulda
|
|
module Matchers
|
|
module ActiveModel
|
|
# @private
|
|
class ExceptionMessageFinder
|
|
def initialize(instance, attribute, context=nil)
|
|
@instance = instance
|
|
@attribute = attribute
|
|
@context = context
|
|
end
|
|
|
|
def allow_description(allowed_values)
|
|
"doesn't raise when #{@attribute} is set to #{allowed_values}"
|
|
end
|
|
|
|
def messages_description
|
|
if has_messages?
|
|
": #{messages.join.inspect}"
|
|
else
|
|
' no exception'
|
|
end
|
|
end
|
|
|
|
def has_messages?
|
|
messages.any?
|
|
end
|
|
|
|
def messages
|
|
@messages ||= validate_and_rescue
|
|
end
|
|
|
|
def source_description
|
|
'exception'
|
|
end
|
|
|
|
def expected_message_from(attribute_message)
|
|
"#{human_attribute_name} #{attribute_message}"
|
|
end
|
|
|
|
private
|
|
|
|
def validate_and_rescue
|
|
@instance.valid?(@context)
|
|
[]
|
|
rescue ::ActiveModel::StrictValidationFailed => exception
|
|
[exception.message]
|
|
end
|
|
|
|
def human_attribute_name
|
|
@instance.class.human_attribute_name(@attribute)
|
|
end
|
|
end
|
|
|
|
end
|
|
end
|
|
end
|
|
|
|
|