Module: ThoughtBot::Shoulda::ActiveRecord::Assertions

Included in:
ActionController::Integration::Session, Test::Unit::TestCase
Defined in:
lib/shoulda/active_record/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_bad_value(object_or_klass, attribute, value, error_message_to_expect = self.class.default_error_message(:invalid)) ⇒ 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 = self.class.default_error_message(:invalid))
  object = get_instance_of(object_or_klass)
  object.send("#{attribute}=", value)
  assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}"
  assert object.errors.on(attribute), "There are no errors on #{attribute} after being set to #{value.inspect}"
  assert_contains(object.errors.on(attribute), error_message_to_expect, "when set to #{value.inspect}")
end

#assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = //) ⇒ Object

Asserts that an Active Record 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")


36
37
38
39
40
41
# File 'lib/shoulda/active_record/assertions.rb', line 36

def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = //)
  object = get_instance_of(object_or_klass)
  object.send("#{attribute}=", value)
  object.valid?
  assert_does_not_contain(object.errors.on(attribute), error_message_to_avoid, "when set to #{value.inspect}")
end

#assert_save(obj) ⇒ Object

Asserts that the given object can be saved

assert_save User.new(params)


8
9
10
11
# File 'lib/shoulda/active_record/assertions.rb', line 8

def assert_save(obj)
  assert obj.save, "Errors: #{pretty_error_messages obj}"
  obj.reload
end

#assert_valid(obj) ⇒ Object

Asserts that the given object is valid

assert_valid User.new(params)


16
17
18
# File 'lib/shoulda/active_record/assertions.rb', line 16

def assert_valid(obj)
  assert obj.valid?, "Errors: #{pretty_error_messages obj}"
end

#pretty_error_messages(obj) ⇒ Object



68
69
70
71
72
73
# File 'lib/shoulda/active_record/assertions.rb', line 68

def pretty_error_messages(obj)
  obj.errors.map do |a, m| 
    msg = "#{a} #{m}" 
    msg << " (#{obj.send(a).inspect})" unless a.to_sym == :base
  end
end