module RSpec::Rails::ControllerExampleGroup::ClassMethods
def controller(base_class = nil, &body)
- Note: - Due to Ruby 1.8 scoping rules in anonymous subclasses, constants
def controller(base_class = nil, &body) if RSpec.configuration.infer_base_class_for_anonymous_controllers? base_class ||= controller_class end base_class ||= defined?(ApplicationController) ? ApplicationController : ActionController::Base new_controller_class = Class.new(base_class) do def self.name root_controller = defined?(ApplicationController) ? ApplicationController : ActionController::Base if superclass == root_controller || superclass.abstract? "AnonymousController" else superclass.name end end end new_controller_class.class_exec(&body) (class << self; self; end).__send__(:define_method, :controller_class) { new_controller_class } before do @orig_routes = routes resource_name = if @controller.respond_to?(:controller_name) @controller.controller_name.to_sym else :anonymous end resource_path = if @controller.respond_to?(:controller_path) @controller.controller_path else resource_name.to_s end resource_module = resource_path.rpartition('/').first.presence resource_as = 'anonymous_' + resource_path.tr('/', '_') self.routes = ActionDispatch::Routing::RouteSet.new.tap do |r| r.draw do resources resource_name, as: resource_as, module: resource_module, path: resource_path end end end after do self.routes = @orig_routes @orig_routes = nil end end