Method: Shoulda::ActiveRecord::Assertions#assert_bad_value

Defined in:
lib/shoulda/active_record/assertions.rb

#assert_bad_value(object_or_klass, attribute, value, error_message_to_expect = nil) ⇒ Object

Asserts that an Active Record model invalidates the passed value by making sure the error_message_to_expect is contained within the list of errors for that attribute.

assert_bad_value(User.new, :email, "invalid")
assert_bad_value(User.new, :ssn, "123", /length/)

If a class is passed as the first argument, a new object will be instantiated before the assertion. If an instance variable exists with the same name as the class (underscored), that object will be used instead.

assert_bad_value(User, :email, "invalid")

product = Product.new(:tangible => true)
assert_bad_value(product, :price, "0")


59
60
61
62
63
64
65
66
# File 'lib/shoulda/active_record/assertions.rb', line 59

def assert_bad_value(object_or_klass, attribute, value,
                     error_message_to_expect = nil)
  object = get_instance_of(object_or_klass)
  matcher = allow_value(value).
              for(attribute).
              with_message(error_message_to_expect)
  assert_rejects(matcher, object)
end