fixed should_require_unique_attributes and improved error messages

git-svn-id: https://svn.thoughtbot.com/plugins/shoulda/trunk@285 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
This commit is contained in:
tsaleh 2008-01-20 21:41:10 +00:00
parent 71ed152ce2
commit 91a3bb5e6a
4 changed files with 18 additions and 9 deletions

View File

@ -38,6 +38,7 @@ module ThoughtBot # :nodoc:
attributes.each do |attribute|
should "require #{attribute} to be set" do
object = klass.new
object.send("#{attribute}=", nil)
assert !object.valid?, "#{klass.name} does not require #{attribute}."
assert object.errors.on(attribute), "#{klass.name} does not require #{attribute}."
assert_contains(object.errors.on(attribute), message)
@ -443,4 +444,4 @@ module ThoughtBot # :nodoc:
include ThoughtBot::Shoulda::Private
end
end
end
end

View File

@ -122,7 +122,9 @@ module ThoughtBot # :nodoc:
end
should "destroy record" do
assert_raises(::ActiveRecord::RecordNotFound) { @record.reload }
assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
@record.reload
end
end
end
end
@ -155,7 +157,7 @@ module ThoughtBot # :nodoc:
end
should "not have errors on @#{res.object}" do
assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:"
assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
end
end
end
@ -183,7 +185,7 @@ module ThoughtBot # :nodoc:
end
should "not have errors on @#{res.object}" do
assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:"
assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
end
end
end

View File

@ -91,7 +91,9 @@ module ThoughtBot # :nodoc:
end
else
should "destroy record" do
assert_raises(::ActiveRecord::RecordNotFound) { @record.reload }
assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
@record.reload
end
end
end
end
@ -117,7 +119,7 @@ module ThoughtBot # :nodoc:
should_assign_to res.object
should "not have errors on @#{res.object}" do
assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:"
assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
end
end
end

View File

@ -67,15 +67,15 @@ module ThoughtBot # :nodoc:
#
# assert_save User.new(params)
def assert_save(obj)
assert obj.save, "Errors: #{obj.errors.full_messages.join('; ')}"
assert obj.save, "Errors: #{pretty_error_messages obj}"
obj.reload
end
# Asserts that the given object is valid
#
# assert_save User.new(params)
# assert_valid User.new(params)
def assert_valid(obj)
assert obj.valid?, "Errors: #{obj.errors.full_messages.join('; ')}"
assert obj.valid?, "Errors: #{pretty_error_messages obj}"
end
# Asserts that the block uses ActionMailer to send emails
@ -95,6 +95,10 @@ module ThoughtBot # :nodoc:
def assert_does_not_send_email(&blk)
assert_sends_email 0, &blk
end
def pretty_error_messages(obj)
obj.errors.map { |a, m| "#{m} (#{obj.send(a).inspect})" }
end
end
end