Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions features/controller_specs/anonymous_controller.feature
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
Feature: anonymous controller

Use the `controller` method to define an anonymous controller derived from
`ApplicationController`. This is useful for specifying behavior like global
error handling.
Use the `controller` method to define an anonymous controller
that will inherit from the described class. 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:
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:
You can disable base type inference:

RSpec.configure do |c|
c.infer_base_class_for_anonymous_controllers = true
Rspec.configure do |c|
c.infer_base_class_for_anonymous_controllers = false
end

describe BaseController do
controller { ... }
# ^^ creates an anonymous subclass of `BaseController`
# ^^ creates an anonymous subclass of `ApplicationController`

Scenario: specify error handling in ApplicationController
Given a file named "spec/controllers/application_controller_spec.rb" with:
Expand Down Expand Up @@ -97,10 +97,6 @@ Feature: anonymous controller
"""ruby
require "spec_helper"

RSpec.configure do |c|
c.infer_base_class_for_anonymous_controllers = true
end

class ApplicationController < ActionController::Base; end

class ApplicationControllerSubclass < ApplicationController; end
Expand Down
7 changes: 3 additions & 4 deletions lib/generators/rspec/install/templates/spec/spec_helper.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ RSpec.configure do |config|
# instead of true.
config.use_transactional_fixtures = true

# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# If false, the base class of anonymous controllers will not be inferred
# automatically.
# config.infer_base_class_for_anonymous_controllers = true

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/rails/example/controller_example_group.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.configure do |config|
config.add_setting :infer_base_class_for_anonymous_controllers, :default => false
config.add_setting :infer_base_class_for_anonymous_controllers, :default => true
end

module RSpec::Rails
Expand Down
4 changes: 4 additions & 0 deletions spec/rspec/rails/example/controller_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ module RSpec::Rails
allow(group).to receive(:controller_class).and_return(Class.new)
end

it "defaults to inferring anonymous controller class" do
expect(RSpec.configuration.infer_base_class_for_anonymous_controllers).to be_truthy
end

it "infers the anonymous controller class when infer_base_class_for_anonymous_controllers is true" do
allow(RSpec.configuration).to receive(:infer_base_class_for_anonymous_controllers?).and_return(true)
group.controller { }
Expand Down