1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

Fix a bug in the pretty_error_messages helper.

* Fixes an issue where matchers would fail on models
with an autosaved belongs_to association.
This commit is contained in:
Henning Koch 2013-03-26 13:59:05 +01:00 committed by Jason Draper
parent f1364514fa
commit d1dc606cea
3 changed files with 19 additions and 1 deletions

View file

@ -1,5 +1,7 @@
# HEAD
* Fix a bug in validations with autosaved models.
* Fix maximum value detection for the `ensure_inclusion_of` and
`ensure_exclusion_of` matchers.

View file

@ -5,7 +5,10 @@ module Shoulda # :nodoc:
def pretty_error_messages(obj) # :nodoc:
obj.errors.map do |attribute, model|
msg = "#{attribute} #{model}"
msg << " (#{obj.send(attribute).inspect})" unless attribute.to_sym == :base
if attribute.to_sym != :base && obj.respond_to?(attribute)
msg << " (#{obj.send(attribute).inspect})"
end
msg
end
end

View file

@ -70,6 +70,19 @@ describe Shoulda::Matchers::ActiveModel::ValidationMessageFinder do
description.should == 'no errors'
end
it 'should not fetch attribute values for errors that were copied from an autosaved belongs_to association' do
instance = define_model(:example) do
validate do |record|
record.errors.add('association.association_attribute', 'is invalid')
end
end.new
finder = Shoulda::Matchers::ActiveModel::ValidationMessageFinder.new(instance, :attribute)
expected_messages = ['association.association_attribute is invalid']
finder.messages_description.should == "errors: #{expected_messages}"
end
end
context '#source_description' do