方法
Attributes
| [W] | helper_class |
实例公共方法
determine_default_helper_class(name) 链接
源文件: 显示 | 在 GitHub 上
# File actionview/lib/action_view/test_case.rb, line 213 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 232 def helper_class @helper_class ||= determine_default_helper_class(name) end
helper_method(*methods) 链接
源文件: 显示 | 在 GitHub 上
# File actionview/lib/action_view/test_case.rb, line 219 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 236 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
参数¶ ↑
格式-
用于渲染内容的格式的名称(作为
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 197 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 204 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