Skip to content

Instantly share code, notes, and snippets.

@mallain
Created May 12, 2010 14:38
Show Gist options
  • Save mallain/398660 to your computer and use it in GitHub Desktop.
Save mallain/398660 to your computer and use it in GitHub Desktop.
# Generate generic tests for instance and unsaved objects
# object as String
# uniqueness_attr as Array
# model_generic_tests("account", %w(name))
def model_generic_tests(object, uniqueness_attr=[])
context "A #{object} instance" do
setup do
@object = Factory(object.to_sym)
end
should "return its name" do
assert @object.name
end
uniqueness_attr.each do |attr|
model_uniqueness_tests(@object, attr)
end
should "not save #{object} without name" do
@object.name = nil
assert !@object.save
end
end
context "A unsaved #{object} instance" do
setup do
@object = Factory.build(object.to_sym, :name => nil)
end
should "not save #{object} without name" do
assert !@object.save
end
end
end
# Generate uniqueness_tests
# object as String
# attribute as String
# model_uniqueness_tests("account", "name")
def model_uniqueness_tests(object, attribute)
should "have unique #{attribute}" do
object = Factory(object.to_sym, attribute.to_sym => "unique")
object_not_unique = Factory.build(object.to_sym, attribute.to_sym => "unique")
assert !object_not_unique.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment