跳至內容 跳至搜尋
命名空間
方法
#
C
P
R
S
包含的模組

常數

INTERNAL_IVARS = [ :@NAME, :@failures, :@assertions, :@__io__, :@_assertion_wrapped, :@_assertions, :@_result, :@_routes, :@controller, :@_controller, :@_request, :@_config, :@_default_form_builder, :@_layouts, :@_files, :@_rendered_views, :@method_name, :@output_buffer, :@_partials, :@passed, :@rendered, :@request, :@routes, :@tagged_logger, :@_templates, :@options, :@test_passed, :@view, :@view_context_class, :@view_flow, :@_subscribers, :@html_document, ]
 

屬性

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

執行個體公開方法

_routes()

方法:已渲染

傳回上次 render 呼叫所渲染的內容。

傳回的物件表現得像字串,但也會公開許多可讓你使用 .register_parser 註冊的格式來剖析內容字串的方法。

預設包括以下解析器

.html

rendered 內容 字串 解析成 HTML。預設情況下,這表示 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

若要將已渲染的內容解析成 Capybara::Simple::Node,請重新註冊一個 :html 解析器,呼叫 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

rendered 內容 字串 解析成 JSON。預設情況下,這表示 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 297
def _routes
  @controller._routes if @controller.respond_to?(:_routes)
end

_test_case()

# File actionview/lib/action_view/test_case.rb, line 214
def _test_case
  controller._test_case
end

config()

# File actionview/lib/action_view/test_case.rb, line 232
def config
  @controller.config if @controller.respond_to?(:config)
end

protect_against_forgery?()

# File actionview/lib/action_view/test_case.rb, line 210
def protect_against_forgery?
  false
end

render(options = {}、local_assigns = {}、&區塊)

# 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

rendered_views()

# File actionview/lib/action_view/test_case.rb, line 242
def rendered_views
  @_rendered_views ||= RenderedViewsCollection.new
end

setup_with_controller()

# 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