File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010
1111For a steady stream of TILs, [ sign up for my newsletter] ( https://tinyletter.com/jbranchaud ) .
1212
13- _ 1033 TILs and counting..._
13+ _ 1034 TILs and counting..._
1414
1515---
1616
@@ -667,6 +667,7 @@ _1033 TILs and counting..._
667667- [ Read-Only Models] ( rails/read-only-models.md )
668668- [ Remove The Default Value On A Column] ( rails/remove-the-default-value-on-a-column.md )
669669- [ Render An Alternative ActionMailer Template] ( rails/render-an-alternative-action-mailer-template.md )
670+ - [ Render The Response Body In Controller Specs] ( rails/render-the-response-body-in-controller-specs.md )
670671- [ Rescue From] ( rails/rescue-from.md )
671672- [ Retrieve An Object If It Exists] ( rails/retrieve-an-object-if-it-exists.md )
672673- [ Rounding Numbers With Precision] ( rails/rounding-numbers-with-precision.md )
Original file line number Diff line number Diff line change 1+ # Render The Response Body In Controller Specs
2+
3+ Controller specs skip the rendering of views by default. If you want to inspect
4+ some aspect of what is rendered in the HTML body of a response
5+ (` response.body ` ), you can include the ` render_views ` directive in that spec.
6+
7+ ``` ruby
8+ require ' rails_helper'
9+
10+ RSpec .describe DashboardController do
11+ describe ' #index' do
12+ render_views
13+
14+ context ' when there is a signed in user' do
15+ it ' includes their email' do
16+ user = User .create(email: ' user@example.com' )
17+
18+ sign_in(user)
19+
20+ get :index
21+
22+ expect(response.body).to include (' user@example.com' )
23+ end
24+ end
25+ end
26+ end
27+ ```
28+
29+ The ` render_views ` directive call can go at the top of a spec, and all views
30+ for all tests will be rendered. Or you can place it in the nested contexts only
31+ where it is needed.
32+
33+ View rendering is skipped by default in an effort to keep tests speedy. To not
34+ unnecessarily slow down your test suite, make sure to use it sparingly and only
35+ in tests where you are actually inspecting ` response.body ` .
You can’t perform that action at this time.
0 commit comments