Module: Shoulda::ActiveModel::Assertions
- Included in:
- Test::Unit::TestCase
- Defined in:
- lib/shoulda/active_model/assertions.rb
Instance Method Summary collapse
-
#assert_bad_value(object_or_klass, attribute, value, error_message_to_expect = nil) ⇒ Object
Asserts that an Active Model model invalidates the passed
value
by making sure theerror_message_to_expect
is contained within the list of errors for that attribute. -
#assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = nil) ⇒ Object
Asserts that an Active Model model validates with the passed
value
by making sure theerror_message_to_avoid
is not contained within the list of errors for that attribute. -
#assert_valid(obj) ⇒ Object
Asserts that the given object is valid.
Instance Method Details
#assert_bad_value(object_or_klass, attribute, value, error_message_to_expect = nil) ⇒ Object
Asserts that an Active Model 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")
51 52 53 54 55 56 57 58 |
# File 'lib/shoulda/active_model/assertions.rb', line 51 def assert_bad_value(object_or_klass, attribute, value, = nil) object = get_instance_of(object_or_klass) matcher = allow_value(value). for(attribute). () assert_rejects(matcher, object) end |
#assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = nil) ⇒ Object
Asserts that an Active Model model validates with the passed value
by making sure the error_message_to_avoid
is not contained within the list of errors for that attribute.
assert_good_value(User.new, :email, "[email protected]")
assert_good_value(User.new, :ssn, "123456789", /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_good_value(User, :email, "[email protected]")
product = Product.new(:tangible => false)
assert_good_value(product, :price, "0")
27 28 29 30 31 32 33 |
# File 'lib/shoulda/active_model/assertions.rb', line 27 def assert_good_value(object_or_klass, attribute, value, = nil) object = get_instance_of(object_or_klass) matcher = allow_value(value). for(attribute). () assert_accepts(matcher, object) end |
#assert_valid(obj) ⇒ Object
Asserts that the given object is valid
assert_valid User.new(params)
7 8 9 |
# File 'lib/shoulda/active_model/assertions.rb', line 7 def assert_valid(obj) assert obj.valid?, "Errors: #{ obj}" end |