Skip to content

Commit d7e401d

Browse files
authored
Merge pull request rails#49474 from seanpdoyle/upgrading-action-view-rendered
Document how to upgrade to `ActionView::TestCase#rendered` [ci skip]
2 parents e81e2e8 + 5243fd3 commit d7e401d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

guides/source/upgrading_ruby_on_rails.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,33 @@ See the [Testing Rails Applications](https://edgeguides.rubyonrails.org/testing.
257257

258258
If you run a single file's tests (`bin/rails test test/models/user_test.rb`), `test:prepare` will not run before it.
259259

260+
### `ActionView::TestCase#rendered` no longer returns a `String`
261+
262+
Starting from Rails 7.1, `ActionView::TestCase#rendered` returns an object that
263+
responds to various format methods (for example, `rendered.html` and
264+
`rendered.json`). To preserve backward compatibility, the object returned from
265+
`rendered` will delegate missing methods to the `String` rendered during the
266+
test. For example, the following [assert_match][] assertion will pass:
267+
268+
```ruby
269+
assert_match /some content/i, rendered
270+
```
271+
272+
However, if your tests rely on `ActionView::TestCase#rendered` returning an
273+
instance of `String`, they will fail. To restore the original behavior, you can
274+
override the `#rendered` method to read from the `@rendered` instance variable:
275+
276+
```ruby
277+
# config/initializers/action_view.rb
278+
279+
ActiveSupport.on_load :action_view_test_case do
280+
attr_reader :rendered
281+
end
282+
```
283+
284+
[assert_match]: https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-assert_match
285+
286+
260287
Upgrading from Rails 6.1 to Rails 7.0
261288
-------------------------------------
262289

0 commit comments

Comments
 (0)