Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extended the model builder helpers to build controllers and requests

  • Loading branch information...
commit ceea356d3d2acfb0f839406731cd9a0fd2e4a1ff 1 parent 39041bf
@jferris jferris authored
Showing with 42 additions and 2 deletions.
  1. +42 −2 test/model_builder.rb
View
44 test/model_builder.rb
@@ -14,8 +14,8 @@ def create_table(table_name, &block)
end
end
- def define_model_class(class_name, &block)
- klass = Class.new(ActiveRecord::Base)
+ def define_constant(class_name, base, &block)
+ klass = Class.new(base)
Object.const_set(class_name, klass)
klass.class_eval(&block) if block_given?
@@ -26,6 +26,10 @@ def define_model_class(class_name, &block)
klass
end
+ def define_model_class(class_name, &block)
+ define_constant(class_name, ActiveRecord::Base, &block)
+ end
+
def define_model(name, columns = {}, &block)
class_name = name.to_s.pluralize.classify
table_name = class_name.tableize
@@ -39,6 +43,34 @@ def define_model(name, columns = {}, &block)
define_model_class(class_name, &block)
end
+ def define_controller(class_name, &block)
+ define_constant(class_name, ActionController::Base, &block)
+ end
+
+ def define_routes(&block)
+ @replaced_routes = ActionController::Routing::Routes
+ new_routes = ActionController::Routing::RouteSet.new
+ silence_warnings do
+ ActionController::Routing.const_set('Routes', new_routes)
+ end
+ new_routes.draw(&block)
+ new_routes.load!
+ end
+
+ def build_response(&block)
+ klass = define_controller('Examples')
+ block ||= lambda { render :nothing => true }
+ klass.class_eval { define_method(:example, &block) }
+ define_routes {|map| map.resources :examples }
+
+ @controller = klass.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ get :example
+
+ @controller
+ end
+
def teardown_with_models
if @defined_constants
@defined_constants.each do |class_name|
@@ -54,6 +86,14 @@ def teardown_with_models
end
end
+ if @replaced_routes
+ ActionController::Routing::Routes.clear!
+ silence_warnings do
+ ActionController::Routing.const_set('Routes', @replaced_routes)
+ end
+ @replaced_routes.reload!
+ end
+
teardown_without_models
end
alias_method :teardown_without_models, :teardown
Please sign in to comment.
Something went wrong with that request. Please try again.