0
@@ -182,9 +182,20 @@ module ThoughtBot # :nodoc:
0
# <tt>value</tt> by making sure the <tt>error_message_to_avoid</tt> is not
0
# contained within the list of errors for that attribute.
0
- # assert_good_value(User.new, :email, "user@example.com") #=> passes
0
- # assert_good_value(User.new, :ssn, "123456789", /length/) #=> passes
0
- def assert_good_value(object, attribute, value, error_message_to_avoid = //)
0
+ # assert_good_value(User.new, :email, "user@example.com")
0
+ # assert_good_value(User.new, :ssn, "123456789", /length/)
0
+ # If a class is passed as the first argument, a new object will be
0
+ # instantiated before the assertion. If an instance variable exists with
0
+ # the same name as the class (underscored), that object will be used
0
+ # assert_good_value(User, :email, "user@example.com")
0
+ # @product = Product.new(:tangible => false)
0
+ # assert_good_value(Product, :price, "0")
0
+ def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = //)
0
+ object = get_instance_of(object_or_klass)
0
object.send("#{attribute}=", value)
0
assert_does_not_contain(object.errors.on(attribute), error_message_to_avoid, "when set to #{value.inspect}")
0
@@ -194,9 +205,20 @@ module ThoughtBot # :nodoc:
0
# <tt>value</tt> by making sure the <tt>error_message_to_expect</tt> is
0
# contained within the list of errors for that attribute.
0
- # assert_bad_value(User.new, :email, "invalid") #=> passes
0
- # assert_bad_value(User.new, :ssn, "123", /length/) #=> passes
0
- def assert_bad_value(object, attribute, value, error_message_to_expect = /invalid/)
0
+ # assert_bad_value(User.new, :email, "invalid")
0
+ # assert_bad_value(User.new, :ssn, "123", /length/)
0
+ # If a class is passed as the first argument, a new object will be
0
+ # instantiated before the assertion. If an instance variable exists with
0
+ # the same name as the class (underscored), that object will be used
0
+ # assert_bad_value(User, :email, "invalid")
0
+ # @product = Product.new(:tangible => true)
0
+ # assert_bad_value(Product, :price, "0")
0
+ def assert_bad_value(object_or_klass, attribute, value, error_message_to_expect = /invalid/)
0
+ object = get_instance_of(object_or_klass)
0
object.send("#{attribute}=", value)
0
assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}"
0
assert object.errors.on(attribute), "There are no errors on #{attribute} after being set to #{value.inspect}"
0
@@ -206,6 +228,17 @@ module ThoughtBot # :nodoc:
0
def pretty_error_messages(obj)
0
obj.errors.map { |a, m| "#{a} #{m} (#{obj.send(a).inspect})" }
0
+ def get_instance_of(object_or_klass)
0
+ if object_or_klass.is_a?(Class)
0
+ klass = object_or_klass
0
+ instance_variable_get("@#{klass.to_s.underscore}") || klass.new
Comments
No one has commented yet.