Created
October 20, 2011 11:55
-
-
Save caleon/1300964 to your computer and use it in GitHub Desktop.
Rails ActiveSupport::TestCase helper method for checking a model record has an error on a field
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
# This method allows you to not have to hardcode validation messages as a part of unit | |
# tests, instead relying, as the ActiveModel::Errors class does, on I18n dictionaries. | |
# Usage: assert_has_error(@new_user, :first_name, :regexp) | |
# Of course, in the above example, :regexp is a custom type of error that I implemented | |
# for my own app, but you can substitute things like :invalid, :blank, :inclusion, etc. | |
def assert_has_error(record, attribute, type = :invalid) | |
record.send(:run_validations!) # This in order to trigger the :validate callback first. | |
assert record.errors[attribute].include?(record.errors.generate_message(attribute, type)), "Expected #{record.class.model_name.human} record to have an error of type :#{type} for attribute #{record.class.human_attribute_name(attribute)}." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment