Skip to content

Commit 9ef4a0f

Browse files
committed
Add Render The Response Body In Controller Specs a rails til
1 parent fcd1cca commit 9ef4a0f

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For 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)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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`.

0 commit comments

Comments
 (0)