module ThoughtBot::Shoulda::ActiveRecord::Macros
def should_ensure_length_is(attribute, length, opts = {})
should_ensure_length_is :ssn, 9
Example:
Regexp or string. Default = I18n.translate('activerecord.errors.messages.wrong_length') % length
* :message - value the test expects to find in errors.on(:attribute).
Options:
create a new instance to test against.
model being tested, then this method will use that. Otherwise, it will
If an instance variable has been created in the setup named after the
Ensures that the length of the attribute is exactly a certain length
def should_ensure_length_is(attribute, length, opts = {}) message = get_options!([opts], :message) message ||= default_error_message(:wrong_length, :count => length) klass = model_class should "not allow #{attribute} to be less than #{length} chars long" do min_value = "x" * (length - 1) assert_bad_value(klass, attribute, min_value, message) end should "not allow #{attribute} to be greater than #{length} chars long" do max_value = "x" * (length + 1) assert_bad_value(klass, attribute, max_value, message) end should "allow #{attribute} to be #{length} chars long" do valid_value = "x" * (length) assert_good_value(klass, attribute, valid_value, message) end end