-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Introduce ActionView::TestCase.register_parser
#49194
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
Introduce ActionView::TestCase.register_parser
#49194
Conversation
39ff775
to
bffea94
Compare
bffea94
to
c1f01c0
Compare
@rafaelfranca this proposal is in the same spirit of the work merged as part of #49003. |
c1f01c0
to
d21f400
Compare
b7442a8
to
c439b22
Compare
a764d3f
to
42a11cb
Compare
c750252
to
cad9fbf
Compare
cad9fbf
to
f61c6ef
Compare
ActionView::TestCase.register_decoder
ActionView::TestCase.register_parser
fe85304
to
1756b97
Compare
1756b97
to
3365450
Compare
956d7af
to
35d963b
Compare
35d963b
to
0de3edb
Compare
0de3edb
to
5894f31
Compare
@@ -108,6 +189,26 @@ def include_helper_modules! | |||
end | |||
end | |||
|
|||
included do | |||
class_attribute :content_class, instance_accessor: false, default: Class.new(Content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rafaelfranca is this class a Content
descendant for troubleshooting and naming purposes? Could it be default: Class.new(SimpleDelegator)
directly, cutting out the Content
class entirely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't need to be a descendant. I just changed that. But I prefer to have a name for debug purpose, and documentation so people can search. I just used the Ruby 3.3 set_temporary_name if it is present.
5894f31
to
14dfdae
Compare
Register a callable to decode rendered content for a given MIME type Each registered decoder will also define a `#rendered.$MIME` helper method, where `$MIME` corresponds to the value of the `mime` argument. === Arguments `mime` - Symbol the MIME Type name for the rendered content `callable` - Callable to decode the String. Accepts the String value as its only argument `block` - Block serves as the decoder when the `callable` is omitted By default, ActionView::TestCase defines a decoder for: * :html - returns an instance of Nokogiri::XML::Node * :json - returns an instance of ActiveSupport::HashWithIndifferentAccess Each pre-registered decoder also defines a corresponding helper: * :html - defines `rendered.html` * :json - defines `rendered.json` === Examples To parse the rendered content into RSS, register a call to `RSS::Parser.parse`: ```ruby register_decoder :rss, -> rendered { RSS::Parser.parse(rendered) } test "renders RSS" do article = Article.create!(title: "Hello, world") render formats: :rss, partial: article assert_equal "Hello, world", rendered.rss.items.last.title end ``` To parse the rendered content into a Capybara::Simple::Node, re-register an `:html` decoder with a call to `Capybara.string`: ```ruby register_decoder :html, -> rendered { Capybara.string(rendered) } test "renders HTML" do article = Article.create!(title: "Hello, world") render partial: article rendered.html.assert_css "h1", text: "Hello, world" end ```
14dfdae
to
7badc42
Compare
Based on a [comment on rails#49194][], describe how to achieve backwards compatibility with existing `ActionView::TestCase` definitions. [comment on rails#49194]: rails#49194 (comment)
Probably stemming from `rendered` having been converted to a proxy object in rails/rails#49194
* Rails 7.1: call `to_s` on `rendered` proxy Probably stemming from `rendered` having been converted to a proxy object in rails/rails#49194 * CI with Rails 7.1 * Don't eager_load on CI * Needs to be moved up further
Motivation / Background
Register a callable to decode rendered content for a given MIME type
Each registered parser will also define a
#rendered.$MIME
helper method, where$MIME
corresponds to the value of themime
argument.Detail
Arguments
mime
- Symbol the MIME Type name for the rendered contentcallable
- Callable to decode the String. Accepts the Stringvalue as its only argument
block
- Block serves as the parser when thecallable
is omittedBy default, ActionView::TestCase defines a parser for:
Each pre-registered parser also defines a corresponding helper:
rendered.html
rendered.json
Examples
To parse the rendered content into RSS, register a call to
RSS::Parser.parse
:To parse the rendered content into a Capybara::Simple::Node, re-register an
:html
parser with a call toCapybara.string
:For the sake of backwards compatibility, re-define existing support for
document_root_element
in terms ofrendered.html
.Additional information
This proposal draws inspiration from ActionDispatch::Testing::RequestEncoder, and the
response.parsed_body
test method that it enables.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]