Created
September 20, 2008 15:46
-
-
Save mattwynne/11764 to your computer and use it in GitHub Desktop.
undefined
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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