Skip to content

Instantly share code, notes, and snippets.

@brianjlandau
Created January 21, 2010 18:30
Show Gist options
  • Select an option

  • Save brianjlandau/283043 to your computer and use it in GitHub Desktop.

Select an option

Save brianjlandau/283043 to your computer and use it in GitHub Desktop.
class ActiveSupport::TestCase
protected
# Use this to destroy ActiveRecord models you created and stored in instance variables.
# Pass in the names of the instance variables:
#
# teardown do
# destroy_and_nil(*%w(contact_details user product))
# end
#
def destroy_and_nil(*args)
args.flatten.each do |ivar|
unless instance_eval("@#{ivar}.nil?")
if instance_eval("@#{ivar}").respond_to?(:destroy!)
instance_eval("@#{ivar}.destroy!")
else
instance_eval("@#{ivar}.destroy")
end
instance_eval("@#{ivar} = nil")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment