module ActionView::TestCase::Behavior
def _routes
def _routes @controller._routes if @controller.respond_to?(:_routes) end
def _test_case
def _test_case controller._test_case end
def _user_defined_ivars
def _user_defined_ivars instance_variables - INTERNAL_IVARS end
def config
def config @controller.config if @controller.respond_to?(:config) end
def document_root_element
def document_root_element Rails::Dom::Testing.html_document.parse(@rendered.blank? ? @output_buffer.to_str : @rendered).root end
def method_missing(selector, *args)
def method_missing(selector, *args) begin routes = @controller.respond_to?(:_routes) && @controller._routes rescue # Don't call routes, if there is an error on _routes call end if routes && (routes.named_routes.route_defined?(selector) || routes.mounted_helpers.method_defined?(selector)) @controller.__send__(selector, *args) else super end end
def protect_against_forgery?
def protect_against_forgery? false end
def render(options = {}, local_assigns = {}, &block)
def render(options = {}, local_assigns = {}, &block) view.assign(view_assigns) @rendered << output = view.render(options, local_assigns, &block) output end
def rendered
assert_pattern { rendered.json => { title: "Hello, world" } }
render formats: :json, partial: "articles/article", locals: { article: article }
article = Article.create!(title: "Hello, world")
test "renders JSON" do
a ActiveSupport::HashWithIndifferentAccess.
Parse the rendered content String into JSON. By default, this means
+.json+
end
rendered.html.assert_css "h1", text: "Hello, world"
render partial: article
article = Article.create!(title: "Hello, world")
test "renders HTML" do
register_parser :html, -> rendered { Capybara.string(rendered) }
Capybara.string:
re-register an :html parser with a call to
To parse the rendered content into a Capybara::Simple::Node,
end
assert_pattern { rendered.html.at("main h1") => { content: "Hello, world" } }
render partial: "articles/article", locals: { article: article }
article = Article.create!(title: "Hello, world")
test "renders HTML" do
a Nokogiri::XML::Node.
Parse the rendered content String into HTML. By default, this means
+.html+
By default includes the following parsers:
.register_parser.
that allows you to parse the content string in formats registered using
The returned object behaves like a string but also exposes a number of methods
Returns the content rendered by the last +render+ call.
def rendered @_rendered ||= self.class.content_class.new(@rendered) end
def rendered_views
def rendered_views @_rendered_views ||= RenderedViewsCollection.new end
def respond_to_missing?(name, include_private = false)
def respond_to_missing?(name, include_private = false) begin routes = defined?(@controller) && @controller.respond_to?(:_routes) && @controller._routes rescue # Don't call routes, if there is an error on _routes call end routes && (routes.named_routes.route_defined?(name) || routes.mounted_helpers.method_defined?(name)) end
def setup_with_controller
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 = +"" test_case_instance = self controller_class.define_method(:_test_case) { test_case_instance } end
def view
def view @view ||= begin view = @controller.view_context view.singleton_class.include(_helpers) view.extend(Locals) view.rendered_views = rendered_views view.output_buffer = output_buffer view end end
def view_assigns
rendered. This is generally intended for internal use and extension
the user in the test case, which are then assigned to the view being
Returns a Hash of instance variables and their values, as defined by
def view_assigns Hash[_user_defined_ivars.map do |ivar| [ivar[1..-1].to_sym, instance_variable_get(ivar)] end] end