Created
January 19, 2009 14:51
-
-
Save splattael/49013 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://gist.github.com/49013 | |
# | |
# assert_errors_on record, :email | |
# assert_errors_on record, :email => 2 | |
# assert_errors_on record, :email => "is blank" | |
# assert_errors_on record, :email => ["is blank", "is invalid"] | |
# assert_errors_on record, :email, :plz => 2 | |
# assert_errors_on record, [:username, :email] => "is blank" | |
def assert_errors_on(object, *attributes_or_hash) | |
assert !object.valid?, "#{object.inspect} should be invalid." | |
hash = attributes_or_hash[-1].is_a?(Hash) ? attributes_or_hash.pop : {} | |
attributes_or_hash.each { |field| hash[field] = 1 } | |
hash.each do |fields, count_or_messages| | |
Array.wrap(fields).each do |field| | |
errors = Array.wrap(object.errors.on(field)) | |
count_or_messages = Array.wrap(count_or_messages) unless count_or_messages.is_a?(Fixnum) | |
case count_or_messages | |
when Fixnum | |
assert_equal count_or_messages, errors.size, | |
"#{count_or_messages} error(s) expected for #{object.class}.#{field} but got #{errors.inspect}." | |
when Array | |
assert_equal count_or_messages.sort, errors.sort, | |
"#{count_or_messages.inspect} error(s) expected for #{object.class}.#{field} but got #{errors.inspect}." | |
else | |
fail "unknown type #{count_or_messages.inspect}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment