Class: Spec::Rails::Example::ControllerExampleGroup
- Inherits:
-
FunctionalExampleGroup
- Object
- ActionController::TestCase
- FunctionalExampleGroup
- Spec::Rails::Example::ControllerExampleGroup
- Defined in:
- lib/spec/rails/example/controller_example_group.rb
Overview
Controller Examples live in $RAILS_ROOT/spec/controllers/.
Controller Examples use Spec::Rails::Example::ControllerExampleGroup, which supports running specs for Controllers in two modes, which represent the tension between the more granular testing common in TDD and the more high level testing built into rails. BDD sits somewhere in between: we want to a balance between specs that are close enough to the code to enable quick fault isolation and far enough away from the code to enable refactoring with minimal changes to the existing specs.
Isolation mode (default)
No dependencies on views because none are ever rendered. The benefit of this mode is that can spec the controller completely independent of the view, allowing that responsibility to be handled later, or by somebody else. Combined w/ separate view specs, this also provides better fault isolation.
Integration mode
To run in this mode, include the integrate_views
declaration in your controller context:
describe ThingController do
integrate_views
...
In this mode, controller specs are run in the same way that rails functional tests run - one set of tests for both the controllers and the views. The benefit of this approach is that you get wider coverage from each spec. Experienced rails developers may find this an easier approach to begin with, however we encourage you to explore using the isolation mode and revel in its benefits.
Expecting Errors
Rspec on Rails will raise errors that occur in controller actions and are not rescued or handeled with rescue_from.
Defined Under Namespace
Modules: ControllerInstanceMethods, TemplateIsolationExtensions
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
-
.controller_name(name) ⇒ Object
When you don’t pass a controller to describe, like this:.
-
.inherited(klass) ⇒ Object
:nodoc:.
-
.integrate_views(integrate_views = true) ⇒ Object
Use integrate_views to instruct RSpec to render views in your controller examples in Integration mode.
-
.integrate_views? ⇒ Boolean
:nodoc:.
-
.set_description(*args) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#bypass_rescue ⇒ Object
Bypasses any error rescues defined with rescue_from.
- #integrate_views? ⇒ Boolean
Methods inherited from FunctionalExampleGroup
#assigns, #cookies, #flash, #orig_assigns, #params, #session, #setup
Methods inherited from ActionController::TestCase
Methods included from RoutingHelpers
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
123 124 125 |
# File 'lib/spec/rails/example/controller_example_group.rb', line 123 def controller @controller end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
123 124 125 |
# File 'lib/spec/rails/example/controller_example_group.rb', line 123 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
123 124 125 |
# File 'lib/spec/rails/example/controller_example_group.rb', line 123 def response @response end |
Class Method Details
.controller_name(name) ⇒ Object
When you don’t pass a controller to describe, like this:
describe ThingsController do
… then you must provide a controller_name within the context of your controller specs:
describe "ThingController" do
controller_name :thing
...
92 93 94 |
# File 'lib/spec/rails/example/controller_example_group.rb', line 92 def controller_name(name) tests "#{name}_controller".camelize.constantize end |
.inherited(klass) ⇒ Object
:nodoc:
64 65 66 67 68 |
# File 'lib/spec/rails/example/controller_example_group.rb', line 64 def inherited(klass) # :nodoc: klass.integrate_views(integrate_views?) klass.subject { controller } super end |
.integrate_views(integrate_views = true) ⇒ Object
Use integrate_views to instruct RSpec to render views in your controller examples in Integration mode.
describe ThingController do
integrate_views
...
See Spec::Rails::Example::ControllerExampleGroup for more information about Integration and Isolation modes.
56 57 58 |
# File 'lib/spec/rails/example/controller_example_group.rb', line 56 def integrate_views(integrate_views = true) @integrate_views = integrate_views end |
.integrate_views? ⇒ Boolean
:nodoc:
60 61 62 |
# File 'lib/spec/rails/example/controller_example_group.rb', line 60 def integrate_views? # :nodoc: @integrate_views end |
.set_description(*args) ⇒ Object
:nodoc:
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/spec/rails/example/controller_example_group.rb', line 70 def set_description(*args) # :nodoc: super if described_class && described_class.ancestors.include?(ActionController::Base) controller_klass = if superclass.controller_class.ancestors.include?(ActionController::Base) superclass.controller_class else described_class end tests controller_klass end end |
Instance Method Details
#bypass_rescue ⇒ Object
Bypasses any error rescues defined with rescue_from. Useful in cases in which you want to specify errors coming out of actions that might be caught by a rescue_from clause that is specified separately.
Note that this will override the effect of rescue_action_in_public
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/spec/rails/example/controller_example_group.rb', line 135 def bypass_rescue if ::Rails::VERSION::STRING >= '2.2' def controller.rescue_action(exception) raise exception end else def controller.rescue_action_with_handler(exception) raise exception end end end |
#integrate_views? ⇒ Boolean
125 126 127 |
# File 'lib/spec/rails/example/controller_example_group.rb', line 125 def integrate_views? @integrate_views || self.class.integrate_views? end |