Added a test helper to build models/database tables for testing matchers

This commit is contained in:
Joe Ferris 2008-12-15 13:13:56 -05:00
parent ba0a96221d
commit 332989351c
2 changed files with 42 additions and 0 deletions

41
test/model_builder.rb Normal file
View File

@ -0,0 +1,41 @@
class Test::Unit::TestCase
def build_model_class(name, columns = {}, &block)
class_name = name.to_s.pluralize.classify
table_name = class_name.tableize
connection = ActiveRecord::Base.connection
begin
connection.execute("DROP TABLE IF EXISTS #{table_name}")
connection.create_table table_name do |t|
columns.each do |name, type|
t.column name, type
end
end
rescue Exception => e
connection.execute("DROP TABLE #{table_name}")
raise e
end
klass = Class.new(ActiveRecord::Base)
Object.const_set(class_name, klass)
klass.class_eval(&block) if block_given?
@built_models ||= []
@built_models << klass
klass
end
def teardown_with_models
if @built_models
@built_models.each do |klass|
ActiveRecord::Base.connection.execute("DROP TABLE #{klass.table_name}")
Object.send(:remove_const, klass.name)
end
end
teardown_without_models
end
alias_method :teardown_without_models, :teardown
alias_method :teardown, :teardown_with_models
end

View File

@ -31,3 +31,4 @@ class Test::Unit::TestCase #:nodoc:
end
require 'test/fail_macros'
require 'test/model_builder'