module ActionView::TestCase::Behavior

Constants

INTERNAL_IVARS

Attributes

controller[RW]
output_buffer[RW]
request[RW]

Public Instance Methods

_routes() click to toggle source
# File lib/action_view/test_case.rb, line 298
def _routes
  @controller._routes if @controller.respond_to?(:_routes)
end
_test_case() click to toggle source
# File lib/action_view/test_case.rb, line 215
def _test_case
  controller._test_case
end
config() click to toggle source
# File lib/action_view/test_case.rb, line 233
def config
  @controller.config if @controller.respond_to?(:config)
end
protect_against_forgery?() click to toggle source
# File lib/action_view/test_case.rb, line 211
def protect_against_forgery?
  false
end
render(options = {}, local_assigns = {}, &block) click to toggle source
# File lib/action_view/test_case.rb, line 237
def render(options = {}, local_assigns = {}, &block)
  view.assign(view_assigns)
  @rendered << output = view.render(options, local_assigns, &block)
  output
end
rendered() click to toggle source

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 lib/action_view/test_case.rb, line 294
def rendered
  @_rendered ||= self.class.content_class.new(@rendered)
end
rendered_views() click to toggle source
# File lib/action_view/test_case.rb, line 243
def rendered_views
  @_rendered_views ||= RenderedViewsCollection.new
end
setup_with_controller() click to toggle source
# File lib/action_view/test_case.rb, line 221
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

Private Instance Methods

_user_defined_ivars() click to toggle source
# File lib/action_view/test_case.rb, line 405
def _user_defined_ivars
  instance_variables - INTERNAL_IVARS
end
_view()
Alias for: view
document_root_element() click to toggle source

Need to experiment if this priority is the best one: rendered => output_buffer

# File lib/action_view/test_case.rb, line 333
def document_root_element
  Rails::Dom::Testing.html_document.parse(@rendered.blank? ? @output_buffer.to_str : @rendered).root
end
method_missing(selector, *args) click to toggle source
Calls superclass method
# File lib/action_view/test_case.rb, line 419
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
respond_to_missing?(name, include_private = false) click to toggle source
# File lib/action_view/test_case.rb, line 436
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
view() click to toggle source

The instance of ActionView::Base that is used by render.

# File lib/action_view/test_case.rb, line 357
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
Also aliased as: _view
view_assigns() click to toggle source

Returns a Hash of instance variables and their values, as defined by the user in the test case, which are then assigned to the view being rendered. This is generally intended for internal use and extension frameworks.

# File lib/action_view/test_case.rb, line 413
def view_assigns
  Hash[_user_defined_ivars.map do |ivar|
    [ivar[1..-1].to_sym, instance_variable_get(ivar)]
  end]
end