Class Spec::Rails::Example::ControllerExampleGroup

  1. lib/spec/rails/example/controller_example_group.rb

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.

Methods

public class

  1. controller_name
  2. integrate_views

public instance

  1. bypass_rescue

protected instance

  1. _assigns_hash_proxy

Attributes

controller [R]
request [R]
response [R]

Public class methods

controller_name (name)

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
  ...
[show source]
# File lib/spec/rails/example/controller_example_group.rb, line 88
          def controller_name(name)
            controller_class = "#{name}_controller".camelize.constantize
            subject { controller_class }
            tests controller_class
          end
integrate_views (integrate_views = true)

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.

[show source]
# File lib/spec/rails/example/controller_example_group.rb, line 56
          def integrate_views(integrate_views = true)
            @integrate_views = integrate_views
          end

Public instance methods

bypass_rescue ()

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

[show source]
# 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

Protected instance methods

_assigns_hash_proxy ()
[show source]
# File lib/spec/rails/example/controller_example_group.rb, line 149
        def   _assigns_hash_proxy_assigns_hash_proxy_assigns_hash_proxy
          @_assigns_hash_proxy ||= AssignsHashProxy.new(self) {@response.template}
        end