Skip to content

Commit

Permalink
Allow testing of ApplicationController subclasses with controller()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Gehard authored and dchelimsky committed Aug 6, 2010
1 parent 59c83c6 commit 48a231e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
76 changes: 57 additions & 19 deletions features/controller_specs/anonymous_controller.feature
Expand Up @@ -6,35 +6,73 @@ Feature: anonymous controller

Scenario: specify error handling in ApplicationController
Given a file named "spec/controllers/application_controller_spec.rb" with:
"""
require "spec_helper"
"""
require "spec_helper"
class ApplicationController < ActionController::Base
class AccessDenied < StandardError; end
class ApplicationController < ActionController::Base
class AccessDenied < StandardError; end
rescue_from AccessDenied, :with => :access_denied
rescue_from AccessDenied, :with => :access_denied
private
private
def access_denied
redirect_to "/401.html"
end
end
def access_denied
redirect_to "/401.html"
describe ApplicationController do
controller do
def index
raise ApplicationController::AccessDenied
end
end
describe ApplicationController do
controller do
def index
raise ApplicationController::AccessDenied
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"

Scenario: specify error handling in subclass of ApplicationController
Given a file named "spec/controllers/application_controller_subclass_spec.rb" with:
"""
require "spec_helper"
class ApplicationController < ActionController::Base
class AccessDenied < StandardError; end
end
class ApplicationControllerSubclass < ApplicationController
rescue_from ApplicationController::AccessDenied, :with => :access_denied
private
def access_denied
redirect_to "/401.html"
end
end
describe ApplicationControllerSubclass do
controller(ApplicationControllerSubclass) 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
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"
15 changes: 12 additions & 3 deletions lib/rspec/rails/example/controller_example_group.rb
Expand Up @@ -112,7 +112,7 @@ def controller_class
# raise ApplicationController::AccessDenied
# end
# end

#
# describe "handling AccessDenied exceptions" do
# it "redirects to the /401.html page" do
# get :index
Expand All @@ -121,14 +121,23 @@ def controller_class
# end
# end
#
# If you would like to test a subclass of ApplicationController, call
# controller like so:
#
# == Example
#
# 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.
def controller(&body)
metadata[:example_group][:describes] = Class.new(ApplicationController, &body)
def controller(base_class = ApplicationController, &body)
metadata[:example_group][:describes] = Class.new(base_class, &body)
metadata[:example_group][:describes].singleton_class.class_eval do
def name
"StubResourcesController"
Expand Down

2 comments on commit 48a231e

@justinko
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused why you would ever need to use controller on a subclass of ApplicationController. The primary reason this method exists is to test ApplicationController because it contains no "actions". I assume your subclasses contain actions (which can be mocked to raise AccessDenied)?

@justinko
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, nevermind, I see the use case in namespaced controllers with the namespace having its own "application_controller"

Please sign in to comment.