方法
屬性
[W] | helper_class |
執行個體公開方法
determine_default_helper_class(name) 連結
來源: 顯示 | 在 GitHub 上
# File actionview/lib/action_view/test_case.rb, line 164 def determine_default_helper_class(name) determine_constant_from_test_name(name) do |constant| Module === constant && !(Class === constant) end end
helper_class() 連結
來源: 顯示 | 在 GitHub 上
# File actionview/lib/action_view/test_case.rb, line 183 def helper_class @helper_class ||= determine_default_helper_class(name) end
helper_method(*methods) 連結
來源: 顯示 | 在 GitHub 上
# File actionview/lib/action_view/test_case.rb, line 170 def helper_method(*methods) # Almost a duplicate from ActionController::Helpers methods.flatten.each do |method| _helpers_for_modification.module_eval <<~end_eval, __FILE__, __LINE__ + 1 def #{method}(...) # def current_user(...) _test_case.send(:'#{method}', ...) # _test_case.send(:'current_user', ...) end # end end_eval end end
new(*) 連結
來源: 顯示 | 在 GitHub 上
# File actionview/lib/action_view/test_case.rb, line 187 def new(*) include_helper_modules! super end
register_parser(format, callable = nil, &block) 連結
為既定範本格式的渲染內容註冊可呼叫的項目。
每個已註冊的解析器亦會定義 #rendered.[FORMAT]
輔助方法,其中 [FORMAT]
對應於 format
參數的值。
預設情況下,
為下列項目定義解析器ActionView::TestCase
-
:html
- 返回Nokogiri::XML::Node
實例 -
:json
- 返回ActiveSupport::HashWithIndifferentAccess
實例
這些預先註冊的解析器亦定義相應的輔助方法
-
:html
- 定義rendered.html
-
:json
- 定義rendered.json
參數
format
-
用於渲染內容的格式名稱(為
Symbol
)。 callable
-
解析器。可呼叫的物件,接受已渲染的字串作為其唯一參數。或者,解析器可以指定為區塊。
範例
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
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
若要將已渲染內容解析成 RSS,請註冊呼叫 RSS::Parser.parse
register_parser :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
若要將已渲染內容解析成 Capybara::Simple::Node
,請使用呼叫 Capybara.string
重新註冊 :html
解析器
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
來源: 顯示 | 在 GitHub 上
# File actionview/lib/action_view/test_case.rb, line 148 def register_parser(format, callable = nil, &block) parser = callable || block || :itself.to_proc content_class.redefine_method(format) do parser.call(to_s) end end
tests(helper_class) 鏈結
來源: 顯示 | 在 GitHub 查看
# File actionview/lib/action_view/test_case.rb, line 155 def tests(helper_class) case helper_class when String, Symbol self.helper_class = "#{helper_class.to_s.underscore}_helper".camelize.safe_constantize when Module self.helper_class = helper_class end end