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

Defined in:
lib/rspec/rails/example/controller_example_group.rb

Instance Method Summary collapse

Instance Method Details

#controller(base_class = ApplicationController, &body) ⇒ Object

Supports a simple DSL for specifying behaviour 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.

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

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

controller(ApplicationControllerSubclass) do
  # ....
end

NOTICE: Due to Ruby 1.8 scoping rules in anoymous 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.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rspec/rails/example/controller_example_group.rb', line 143

def controller(base_class = ApplicationController, &body)
  [:example_group][:describes] = Class.new(base_class, &body)
  [:example_group][:describes].singleton_class.class_eval do
    def name
      "StubResourcesController"
    end
  end

  before do
    @orig_routes, @routes = @routes, ActionDispatch::Routing::RouteSet.new
    @routes.draw { resources :stub_resources }
  end

  after do
    @routes = @orig_routes
  end
end

#controller_classObject



103
104
105
# File 'lib/rspec/rails/example/controller_example_group.rb', line 103

def controller_class
  describes
end