Skip to content

Latest commit

 

History

History
35 lines (26 loc) · 1.03 KB

render-the-response-body-in-controller-specs.md

File metadata and controls

35 lines (26 loc) · 1.03 KB

Render The Response Body In Controller Specs

Controller specs skip the rendering of views by default. If you want to inspect some aspect of what is rendered in the HTML body of a response (response.body), you can include the render_views directive in that spec.

require 'rails_helper'

RSpec.describe DashboardController do
  describe '#index' do
    render_views

    context 'when there is a signed in user' do
      it 'includes their email' do
        user = User.create(email: 'user@example.com')

        sign_in(user)

        get :index

        expect(response.body).to include('user@example.com')
      end
    end
  end
end

The render_views directive call can go at the top of a spec, and all views for all tests will be rendered. Or you can place it in the nested contexts only where it is needed.

View rendering is skipped by default in an effort to keep tests speedy. To not unnecessarily slow down your test suite, make sure to use it sparingly and only in tests where you are actually inspecting response.body.