forked from rspec/rspec-rails
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2caa56
commit 58e89df
Showing
1 changed file
with
99 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,143 +1,152 @@ | ||
Feature: anonymous controller | ||
|
||
Use the `controller` method to define an anonymous controller derived from | ||
ApplicationController, or any other base controller. This is useful for | ||
specifying behavior like global error handling. | ||
`ApplicationController`. This is useful for specifying behavior like global | ||
error handling. | ||
|
||
To specify a different base class, you can pass the class explicitly to the | ||
controller method: | ||
|
||
controller(BaseController) | ||
|
||
You can also configure RSpec to use the described class: | ||
|
||
RSpec.configure do |c| | ||
c.infer_base_class_for_anonymous_controllers = true | ||
end | ||
|
||
describe BaseController do | ||
controller { ... } | ||
# ^^ creates an anonymous subclass of `BaseController` | ||
|
||
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" | ||
def access_denied | ||
redirect_to "/401.html" | ||
end | ||
end | ||
end | ||
describe ApplicationController do | ||
controller do | ||
def index | ||
raise ApplicationController::AccessDenied | ||
describe ApplicationController do | ||
controller do | ||
def index | ||
raise ApplicationController::AccessDenied | ||
end | ||
end | ||
end | ||
describe "handling AccessDenied exceptions" do | ||
it "redirects to the /401.html page" do | ||
get :index | ||
response.should redirect_to("/401.html") | ||
describe "handling AccessDenied exceptions" do | ||
it "redirects to the /401.html page" do | ||
get :index | ||
response.should redirect_to("/401.html") | ||
end | ||
end | ||
end | ||
end | ||
""" | ||
""" | ||
When I run `rspec spec` | ||
Then the examples should all pass | ||
|
||
Scenario: specify error handling in subclass of ApplicationController | ||
Given a file named "spec/controllers/application_controller_subclass_spec.rb" with: | ||
""" | ||
require "spec_helper" | ||
""" | ||
require "spec_helper" | ||
class ApplicationController < ActionController::Base | ||
class AccessDenied < StandardError; end | ||
end | ||
class ApplicationController < ActionController::Base | ||
class AccessDenied < StandardError; end | ||
end | ||
class ApplicationControllerSubclass < ApplicationController | ||
class ApplicationControllerSubclass < ApplicationController | ||
rescue_from ApplicationController::AccessDenied, :with => :access_denied | ||
rescue_from ApplicationController::AccessDenied, :with => :access_denied | ||
private | ||
def access_denied | ||
redirect_to "/401.html" | ||
def access_denied | ||
redirect_to "/401.html" | ||
end | ||
end | ||
end | ||
describe ApplicationControllerSubclass do | ||
controller(ApplicationControllerSubclass) do | ||
def index | ||
raise ApplicationController::AccessDenied | ||
describe ApplicationControllerSubclass do | ||
controller(ApplicationControllerSubclass) do | ||
def index | ||
raise ApplicationController::AccessDenied | ||
end | ||
end | ||
end | ||
describe "handling AccessDenied exceptions" do | ||
it "redirects to the /401.html page" do | ||
get :index | ||
response.should redirect_to("/401.html") | ||
describe "handling AccessDenied exceptions" do | ||
it "redirects to the /401.html page" do | ||
get :index | ||
response.should redirect_to("/401.html") | ||
end | ||
end | ||
end | ||
end | ||
""" | ||
""" | ||
When I run `rspec spec` | ||
Then the examples should all pass | ||
|
||
Scenario: base class can be inferred | ||
Given a file named "spec/support/base_class_is_inferred_config.rb" with: | ||
""" | ||
require "spec_helper" | ||
RSpec.configure do |c| | ||
c.infer_base_class_for_anonymous_controllers = true | ||
end | ||
""" | ||
And a file named "spec/controllers/base_class_can_be_inferred_spec.rb" with: | ||
""" | ||
require "spec_helper" | ||
class ApplicationController < ActionController::Base | ||
end | ||
class ApplicationControllerSubclass < ApplicationController | ||
end | ||
describe ApplicationControllerSubclass do | ||
controller do | ||
def index | ||
render :text => "Hello World" | ||
end | ||
Scenario: infer base class from the described class | ||
Given a file named "spec/controllers/base_class_can_be_inferred_spec.rb" with: | ||
""" | ||
require "spec_helper" | ||
RSpec.configure do |c| | ||
c.infer_base_class_for_anonymous_controllers = true | ||
end | ||
it "creates an anonymous controller that inherits from ApplicationControllerSubclass" do | ||
controller.should be_a_kind_of(ApplicationControllerSubclass) | ||
class ApplicationController < ActionController::Base; end | ||
class ApplicationControllerSubclass < ApplicationController; end | ||
describe ApplicationControllerSubclass do | ||
controller do | ||
def index | ||
render :text => "Hello World" | ||
end | ||
end | ||
it "creates an anonymous controller derived from ApplicationControllerSubclass" do | ||
controller.should be_a_kind_of(ApplicationControllerSubclass) | ||
end | ||
end | ||
end | ||
""" | ||
""" | ||
When I run `rspec spec` | ||
Then the examples should all pass | ||
|
||
Scenario: regression with ApplicationController around_filters | ||
Scenario: invoke around filter in base class | ||
Given a file named "spec/controllers/application_controller_around_filter_spec.rb" with: | ||
""" | ||
require "spec_helper" | ||
""" | ||
require "spec_helper" | ||
class ApplicationController < ActionController::Base | ||
around_filter :some_around_filter | ||
class ApplicationController < ActionController::Base | ||
around_filter :an_around_filter | ||
def some_around_filter | ||
@callback_invoked = true | ||
yield | ||
def an_around_filter | ||
@callback_invoked = true | ||
yield | ||
end | ||
end | ||
end | ||
describe ApplicationController do | ||
controller do | ||
def index | ||
render :nothing => true | ||
describe ApplicationController do | ||
controller do | ||
def index | ||
render :nothing => true | ||
end | ||
end | ||
end | ||
it "invokes the callback" do | ||
get :index | ||
it "invokes the callback" do | ||
get :index | ||
assigns[:callback_invoked].should be_true | ||
assigns[:callback_invoked].should be_true | ||
end | ||
end | ||
end | ||
""" | ||
""" | ||
When I run `rspec spec` | ||
Then the examples should all pass |