Skip to content

Commit

Permalink
Add controller DSL for spec'ing behaviour of subclasses of
Browse files Browse the repository at this point in the history
ApplicationController.
  • Loading branch information
dchelimsky committed Jul 24, 2010
1 parent 67dfdde commit d4fa068
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
40 changes: 40 additions & 0 deletions features/controller_specs/anonymous_controller.feature
@@ -0,0 +1,40 @@
Feature: anonymous controller

As a Rails developer using RSpec
In order to specify behaviour of ApplicationController
I want a simple DSL for generating anonymous subclasses

Scenario: specify error handling in ApplicationController
Given a file named "spec/controllers/application_controller_spec.rb" with:
"""
require "spec_helper"
class ApplicationController < ActionController::Base
class AccessDenied < StandardError; end
rescue_from AccessDenied, :with => :access_denied
private
def access_denied
redirect_to "/401.html"
end
end
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
"""
When I run "rspec spec"
Then the output should contain "1 example, 0 failures"
48 changes: 48 additions & 0 deletions lib/rspec/rails/example/controller_example_group.rb
Expand Up @@ -89,6 +89,54 @@ module ClassMethods
def controller_class
describes
end

# 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
# <tt>config/routes.rb</tt>.
#
# Supports a simple DSL for specifying behaviour of ApplicationController.
#
# == Example
#
# 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
#
# 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.
def controller(&body)
metadata[:example_group][:describes] = Class.new(ApplicationController, &body)
metadata[: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
end

module InstanceMethods
Expand Down

0 comments on commit d4fa068

Please sign in to comment.