Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Action View Test Case rendered memoization #51093

Merged
merged 2 commits into from Feb 15, 2024

Conversation

seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Feb 15, 2024

Motivation / Background

The introduction of memoization as an optimization posed a backwards incompatible change to View tests that call render multiple times.

Detail

Follow-up to 49856
Follow-up to 49194

This commit changes the @rendered instance variable from a String to an instance of the RenderedViewContent specialized String subclass.

The end result is that there is no memoization to reset, and the memoization optimization side-effect is preserved after rendering for test cases where rendered (or parser methods like rendered.html) might be invoked more than once.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@seanpdoyle seanpdoyle force-pushed the action-view-rendered-memoization branch 2 times, most recently from c8a39b9 to de23549 Compare February 15, 2024 00:34
Follow-up to [49856][]
Follow-up to [49194][]

The introduction of memoization as an optimization posed a backwards
incompatible change to View tests that call `render` multiple times.

This commit changes the `@rendered` instance variable from a `String` to
an instance of the `RenderedViewContent` specialized `String` subclass.

The end result is that there is no memoization to reset, and the
memoization optimization side-effect is preserved after rendering for
test cases where `rendered` (or parser methods like `rendered.html`)
might be invoked more than once.

[49856]: rails#49856 (comment)
[49194]: https://github.com/rails/rails/pull/49194/files#diff-ce84a807f3491121a5230d37bd40454bb1407fcca71179e1a2fa76d4c0ddfa2aR293
@seanpdoyle seanpdoyle force-pushed the action-view-rendered-memoization branch from de23549 to 520cd8f Compare February 15, 2024 01:40
@rails-bot rails-bot bot added the docs label Feb 15, 2024
@eugeneius eugeneius merged commit 9aeb1de into rails:main Feb 15, 2024
4 checks passed
eugeneius added a commit that referenced this pull request Feb 15, 2024
…ation

Action View Test Case `rendered` memoization
@eugeneius
Copy link
Member

Thank you Sean! Backported to 7-1-stable in ddb959b.

@seanpdoyle seanpdoyle deleted the action-view-rendered-memoization branch February 15, 2024 12:34
@@ -283,30 +283,6 @@ const fileInputSelector = Rails.fileInputSelector
Rails.fileInputSelector(...)
```

### `ActionView::TestCase#rendered` no longer returns a `String`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaelfranca https://github.com/rails/rails/pull/49856/files#diff-ce84a807f3491121a5230d37bd40454bb1407fcca71179e1a2fa76d4c0ddfa2aR302 changed that class inheritance hierarchy by making it inherit from String. That was back ported to 7-1-stable (in f7255ec, maybe? That commit's contents differ from its title line).

With that change in class hierarchy, this guidance became partly incorrect, since "ActionView::TestCase#rendered no longer returns a String" was no longer true. With the change in this commit from a method to an attr_reader, that guidance became entirely incorrect, since it's both a String and an attr_reader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants