mirror of https://github.com/rails/rails
7badc42723
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 ``` |
||
---|---|---|
.. | ||
actionpack | ||
comments | ||
customers | ||
db_definitions | ||
developers | ||
digestor | ||
fun | ||
games | ||
good_customers | ||
helpers | ||
layout_tests/alt | ||
layouts | ||
mascots | ||
override/test | ||
override2/layouts/test | ||
projects | ||
public | ||
replies | ||
respond_to | ||
test | ||
topics | ||
translations/templates | ||
_top_level_partial.html.erb | ||
_top_level_partial_only.erb | ||
companies.yml | ||
company.rb | ||
developer.rb | ||
developers.yml | ||
developers_projects.yml | ||
mascot.rb | ||
mascots.yml | ||
plain_text.raw | ||
plain_text_with_characters.raw | ||
project.rb | ||
projects.yml | ||
replies.yml | ||
reply.rb | ||
ruby_template.ruby | ||
shared.html.erb | ||
topic.rb | ||
topics.yml | ||
with_format.json.erb |