123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Rails::ControllerExampleGroup::ClassMethods

Relationships & Source Files
Defined in: rspec-rails/lib/rspec/rails/example/controller_example_group.rb

Overview

Class-level DSL for controller specs.

Instance Method Summary

Instance Method Details

#controller(base_class = nil, &body)

Note:

Due to Ruby 1.8 scoping rules in anonymous subclasses, constants defined in ApplicationController must be fully qualified (e.g. ApplicationController::AccessDenied) in the block passed to the controller method. Any instance methods, filters, etc, that are defined in ApplicationController, however, are accessible from within the block.

Supports a simple DSL for specifying behavior of ApplicationController. Creates an anonymous subclass of ApplicationController and evals the body in that context. Also sets up implicit routes for this controller, that are separate from those defined in “config/routes.rb”.

If you would like to spec a subclass of ApplicationController, call controller like so:

controller(ApplicationControllerSubclass) do
  # ....
end

Examples:

describe ApplicationController do
  controller do
    def index
      raise ApplicationController::AccessDenied
    end
  end

  describe "handling AccessDenied exceptions" do
    it "redirects to the /401.html page" do
      get :index
      response.should redirect_to("/401.html")
    end
  end
end
[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 61

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

#controller_class

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 23

def controller_class
  described_class
end

#routes

Specifies the routeset that will be used for the example group. This is most useful when testing ::RSpec::Rails engines.

Examples:

describe MyEngine::PostsController do
  routes { MyEngine::Engine.routes }

  # ...
end
[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 119

def routes
  before do
    self.routes = yield
  end
end