2009-03-18 22:35:43 +00:00
|
|
|
class ActiveSupport::TestCase
|
2010-02-13 03:05:12 +00:00
|
|
|
|
|
|
|
TMP_VIEW_PATH =
|
2010-06-07 21:18:36 +00:00
|
|
|
File.expand_path(File.join(File.dirname(__FILE__), 'rails3_root', 'tmp', 'views')).freeze
|
2010-02-13 03:05:12 +00:00
|
|
|
|
2009-01-24 21:34:09 +00:00
|
|
|
def create_table(table_name, &block)
|
2008-12-15 18:13:56 +00:00
|
|
|
connection = ActiveRecord::Base.connection
|
|
|
|
|
|
|
|
begin
|
|
|
|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
2009-01-24 21:23:11 +00:00
|
|
|
connection.create_table(table_name, &block)
|
|
|
|
@created_tables ||= []
|
|
|
|
@created_tables << table_name
|
2009-01-25 21:19:07 +00:00
|
|
|
connection
|
2008-12-15 18:13:56 +00:00
|
|
|
rescue Exception => e
|
2009-01-24 21:23:11 +00:00
|
|
|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
2008-12-15 18:13:56 +00:00
|
|
|
raise e
|
|
|
|
end
|
2009-01-24 21:23:11 +00:00
|
|
|
end
|
2008-12-15 18:13:56 +00:00
|
|
|
|
2009-01-31 23:42:48 +00:00
|
|
|
def define_constant(class_name, base, &block)
|
2009-02-03 23:22:57 +00:00
|
|
|
class_name = class_name.to_s.camelize
|
|
|
|
|
2009-01-31 23:42:48 +00:00
|
|
|
klass = Class.new(base)
|
2008-12-15 18:13:56 +00:00
|
|
|
Object.const_set(class_name, klass)
|
|
|
|
|
|
|
|
klass.class_eval(&block) if block_given?
|
|
|
|
|
2009-01-24 21:23:11 +00:00
|
|
|
@defined_constants ||= []
|
|
|
|
@defined_constants << class_name
|
2008-12-15 18:13:56 +00:00
|
|
|
|
|
|
|
klass
|
|
|
|
end
|
|
|
|
|
2009-01-31 23:42:48 +00:00
|
|
|
def define_model_class(class_name, &block)
|
|
|
|
define_constant(class_name, ActiveRecord::Base, &block)
|
|
|
|
end
|
|
|
|
|
2009-01-24 21:23:11 +00:00
|
|
|
def define_model(name, columns = {}, &block)
|
|
|
|
class_name = name.to_s.pluralize.classify
|
|
|
|
table_name = class_name.tableize
|
|
|
|
|
2009-01-24 21:34:09 +00:00
|
|
|
create_table(table_name) do |table|
|
2009-01-24 21:23:11 +00:00
|
|
|
columns.each do |name, type|
|
|
|
|
table.column name, type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
define_model_class(class_name, &block)
|
|
|
|
end
|
|
|
|
|
2010-06-08 16:15:52 +00:00
|
|
|
def define_mailer(name, paths, &block)
|
|
|
|
class_name = name.to_s.pluralize.classify
|
|
|
|
klass = define_constant(class_name, ActionMailer::Base, &block)
|
|
|
|
end
|
|
|
|
|
2009-01-31 23:42:48 +00:00
|
|
|
def define_controller(class_name, &block)
|
2009-02-03 23:22:57 +00:00
|
|
|
class_name = class_name.to_s
|
|
|
|
class_name << 'Controller' unless class_name =~ /Controller$/
|
2009-01-31 23:42:48 +00:00
|
|
|
define_constant(class_name, ActionController::Base, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def define_routes(&block)
|
2010-06-07 21:18:36 +00:00
|
|
|
Rails.application.routes.draw(&block)
|
2009-01-31 23:42:48 +00:00
|
|
|
end
|
|
|
|
|
2010-02-13 03:05:12 +00:00
|
|
|
def build_response(opts = {}, &block)
|
|
|
|
action = opts[:action] || 'example'
|
2009-01-31 23:42:48 +00:00
|
|
|
klass = define_controller('Examples')
|
|
|
|
block ||= lambda { render :nothing => true }
|
2010-06-07 21:18:36 +00:00
|
|
|
klass.class_eval { layout false; define_method(action, &block) }
|
2009-02-05 03:04:08 +00:00
|
|
|
define_routes do |map|
|
2010-02-13 03:05:12 +00:00
|
|
|
map.connect 'examples', :controller => 'examples', :action => action
|
2009-02-05 03:04:08 +00:00
|
|
|
end
|
2009-01-31 23:42:48 +00:00
|
|
|
|
2010-02-13 03:05:12 +00:00
|
|
|
create_view("examples/#{action}.html.erb", "abc")
|
|
|
|
klass.view_paths = [TMP_VIEW_PATH]
|
|
|
|
|
2009-01-31 23:42:48 +00:00
|
|
|
@controller = klass.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2010-06-07 21:18:36 +00:00
|
|
|
@controller.send :assign_shortcuts, @request, @response
|
|
|
|
@controller.send :initialize_template_class, @response
|
2010-02-13 03:05:12 +00:00
|
|
|
get action
|
2009-01-31 23:42:48 +00:00
|
|
|
|
|
|
|
@controller
|
|
|
|
end
|
|
|
|
|
2010-02-13 03:05:12 +00:00
|
|
|
def create_view(path, contents)
|
|
|
|
full_path = File.join(TMP_VIEW_PATH, path)
|
|
|
|
FileUtils.mkdir_p(File.dirname(full_path))
|
|
|
|
File.open(full_path, 'w') { |file| file.write(contents) }
|
|
|
|
end
|
|
|
|
|
2008-12-15 18:13:56 +00:00
|
|
|
def teardown_with_models
|
2009-01-24 21:23:11 +00:00
|
|
|
if @defined_constants
|
|
|
|
@defined_constants.each do |class_name|
|
|
|
|
Object.send(:remove_const, class_name)
|
2008-12-15 18:13:56 +00:00
|
|
|
end
|
|
|
|
end
|
2009-01-24 21:23:11 +00:00
|
|
|
|
|
|
|
if @created_tables
|
|
|
|
@created_tables.each do |table_name|
|
|
|
|
ActiveRecord::Base.
|
|
|
|
connection.
|
|
|
|
execute("DROP TABLE IF EXISTS #{table_name}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-13 03:05:12 +00:00
|
|
|
FileUtils.rm_rf(TMP_VIEW_PATH)
|
|
|
|
|
2010-06-07 21:18:36 +00:00
|
|
|
Rails.application.routes_reloader.reload!
|
|
|
|
|
2008-12-15 18:13:56 +00:00
|
|
|
teardown_without_models
|
|
|
|
end
|
|
|
|
alias_method :teardown_without_models, :teardown
|
|
|
|
alias_method :teardown, :teardown_with_models
|
|
|
|
end
|