Skip to content

Instantly share code, notes, and snippets.

@szimek
Created January 5, 2010 20:33
Show Gist options
  • Save szimek/269689 to your computer and use it in GitHub Desktop.
Save szimek/269689 to your computer and use it in GitHub Desktop.
Shoulda macro for validates_existence gem
# Copied from http://github.com/shuber/validates_existence,
# modified to be compatible with the latest (2.10.2) version of shoulda
class Test::Unit::TestCase
def self.should_validate_existence_of(*associations)
allow_nil = get_options!(associations, :allow_nil)
associations.each do |association|
should "require #{association} exists" do
reflection = subject.class.reflect_on_association(association)
object = subject
object.send("#{reflection.primary_key_name}=", 0)
assert !object.valid?, "#{subject.class} was saved with a non-existent #{association}"
assert object.errors.on(association), "There are no errors on #{association} after being set to a non-existent record"
assert_contains(object.errors.on(association), "does not exist", "when set to 0")
end
end
if allow_nil
associations.each do |association|
should "allow #{association} to be nil" do
reflection = subject.class.reflect_on_association(association)
object = subject
object.send("#{reflection.primary_key_name}=", nil)
object.valid?
assert !object.errors.on(association), "There were errors on #{association} after being set to nil"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment