Skip to content
  • Sean Doyle's avatar
    7badc427
    Introduce `ActionView::TestCase.register_parser` · 7badc427
    Sean Doyle authored
    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
    ```
    7badc427
    Introduce `ActionView::TestCase.register_parser`
    Sean Doyle authored
    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
    ```
Loading