module ActionView::TestCase::Behavior::ClassMethods
def determine_default_helper_class(name)
def determine_default_helper_class(name) determine_constant_from_test_name(name) do |constant| Module === constant && !(Class === constant) end end
def helper_class
def helper_class @helper_class ||= determine_default_helper_class(name) end
def helper_method(*methods)
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}(*args, &block) # def current_user(*args, &block) _test_case.send(:'#{method}', *args, &block) # _test_case.send(:'current_user', *args, &block) end # end ruby2_keywords(:'#{method}') end_eval end end
def include_helper_modules!
def include_helper_modules! helper(helper_class) if helper_class include _helpers end
def inherited(descendant) # :nodoc:
def inherited(descendant) # :nodoc: super descendant_content_class = content_class.dup if descendant_content_class.respond_to?(:set_temporary_name) descendant_content_class.set_temporary_name("rendered_content") end descendant.content_class = descendant_content_class end
def new(*)
def new(*) include_helper_modules! super end
def register_parser(format, callable = nil, &block)
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) }
re-register an +:html+ parser with a call to +Capybara.string+:
To parse the rendered content into a +Capybara::Simple::Node+,
end
assert_equal "Hello, world", rendered.rss.items.last.title
render formats: :rss, partial: article
article = Article.create!(title: "Hello, world")
test "renders RSS" do
register_parser :rss, -> rendered { RSS::Parser.parse(rendered) }
To parse the rendered content into RSS, register a call to +RSS::Parser.parse+:
end
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
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
==== Examples
block.
its sole argument. Alternatively, the parser can be specified as a
The parser. A callable object that accepts the rendered string as
[+callable+]
The name (as a +Symbol+) of the format used to render the content.
[+format+]
==== Parameters
* +:json+ - defines +rendered.json+
* +:html+ - defines +rendered.html+
These pre-registered parsers also define corresponding helpers:
* +:json+ - returns an instance of ActiveSupport::HashWithIndifferentAccess
* +:html+ - returns an instance of +Nokogiri::XML::Node+
By default, ActionView::TestCase defines parsers for:
+format+ argument.
method, where +[FORMAT]+ corresponds to the value of the
Each registered parser will also define a +#rendered.[FORMAT]+ helper
format.
Register a callable to parse rendered content for a given template
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
def tests(helper_class)
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