register_parser

- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
register_parser(format, callable = nil, &block)
public
Register a callable to parse rendered content for a given template format.
Each registered parser will also define a +#rendered.[FORMAT]+ helper method, where +[FORMAT]+ corresponds to the value of the format argument.
By default, ActionView::TestCase defines parsers for:
-
:html - returns an instance of +Nokogiri::XML::Node+
-
:json - returns an instance of ActiveSupport::HashWithIndifferentAccess
These pre-registered parsers also define corresponding helpers:
-
:html - defines rendered.html
-
:json - defines rendered.json
Parameters
- format
-
The name (as a Symbol) of the format used to render the content.
- callable
-
The parser. A callable object that accepts the rendered string as its sole argument. Alternatively, the parser can be specified as a block.
Examples
test "renders HTML" do article = Article.create!(title: "Hello, world") render partial: "articles/article", locals: { article: article } assert_pattern { rendered.html.at("main h1") => { content: "Hello, world" } } end test "renders JSON" do article = Article.create!(title: "Hello, world") render formats: :json, partial: "articles/article", locals: { article: article } assert_pattern { rendered.json => { title: "Hello, world" } } end
To parse the rendered content into RSS, register a call to +RSS::Parser.parse+:
register_parser :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 parser with a call to Capybara.string:
register_parser :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