Logo: Relish

  1. Sign in

Project: RSpec Rails 3.4

Controller specs

Controller specs are marked by :type => :controller or if you have set
config.infer_spec_type_from_file_location! by placing them in spec/controllers.

A controller spec is an RSpec wrapper for a Rails functional test
It allows you to simulate a single http request in each example, and then
specify expected outcomes such as:

  • rendered templates
  • redirects
  • instance variables assigned in the controller to be shared with the view
  • cookies sent back with the response

To specify outcomes, you can use:

  • standard rspec matchers (expect(response.status).to eq(200))
  • standard test/unit assertions (assert_equal 200, response.status)
  • rails assertions (assert_response 200)
  • rails-specific matchers:

    • render_template

      expect(response).to render_template(:new)   # wraps assert_template
    • redirect_to

      expect(response).to redirect_to(location)   # wraps assert_redirected_to
    • have_http_status

      expect(response).to have_http_status(:created)
    • be_a_new

      expect(assigns(:widget)).to be_a_new(Widget)


RSpec.describe TeamsController do
  describe "GET index" do
    it "assigns @teams" do
      team = Team.create
      get :index
      expect(assigns(:teams)).to eq([team])

    it "renders the index template" do
      get :index
      expect(response).to render_template("index")



  1. Cookies
  2. controller spec
  3. views are stubbed by default
  4. render_views
  5. anonymous controller
  6. bypass rescue
  7. engine routes for controllers

Last published about 7 years ago by myronmarston.