module ActionView::TestCase::Behavior
Constants
- INTERNAL_IVARS
Attributes
controller[RW]
output_buffer[RW]
rendered[RW]
request[RW]
Public Instance Methods
# File actionview/lib/action_view/test_case.rb, line 297 def _routes @controller._routes if @controller.respond_to?(:_routes) end
:method: rendered
Returns the content rendered by the last render call.
The returned object behaves like a string but also exposes a number of methods that allows you to parse the content string in formats registered using .register_parser.
By default includes the following parsers:
.html
Parse the rendered content String into HTML. By default, this means a Nokogiri::XML::Node.
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
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
.json
Parse the rendered content String into JSON. By default, this means a ActiveSupport::HashWithIndifferentAccess.
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
# File actionview/lib/action_view/test_case.rb, line 214 def _test_case controller._test_case end
# File actionview/lib/action_view/test_case.rb, line 232 def config @controller.config if @controller.respond_to?(:config) end
# File actionview/lib/action_view/test_case.rb, line 210 def protect_against_forgery? false end
# File actionview/lib/action_view/test_case.rb, line 236
def render(options = {}, local_assigns = {}, &block)
view.assign(view_assigns)
@rendered << output = view.render(options, local_assigns, &block)
output
end # File actionview/lib/action_view/test_case.rb, line 242 def rendered_views @_rendered_views ||= RenderedViewsCollection.new end
# File actionview/lib/action_view/test_case.rb, line 220
def setup_with_controller
controller_class = Class.new(ActionView::TestCase::TestController)
@controller = controller_class.new
@request = @controller.request
@view_flow = ActionView::OutputFlow.new
@output_buffer = ActionView::OutputBuffer.new
@rendered = self.class.content_class.new(+"")
test_case_instance = self
controller_class.define_method(:_test_case) { test_case_instance }
end
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.