Skip to content

Instantly share code, notes, and snippets.

@mattwynne
Created September 20, 2008 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattwynne/11764 to your computer and use it in GitHub Desktop.
Save mattwynne/11764 to your computer and use it in GitHub Desktop.
undefined
# put this in your features/steps/env.rb to clean up any AR objects created during a scenario before the next one runs
module CleanUp
class << self
attr_accessor :log
end
def self.record(obj)
@objects ||= {}
@objects[obj.class] ||= []
@objects[obj.class] << obj
end
def self.clean
clean_data
end
private
def self.clean_data
ActiveRecord::Base.connection.disable_referential_integrity do
print "cleaning up database rows: " if @log
$stdout.flush if @log
(@objects||[]).each do |klass, os|
print "#{os.length} #{klass}, " if @log
os.each(&:destroy)
end
puts if @log
end
@objects = {}
end
end
class ActiveRecord::Base
def before_create
CleanUp.record(self)
end
end
# after every scenario:
After do
CleanUp.clean
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment