Module: ActionView::TestCase::Behavior::ClassMethods
Relationships & Source Files | |
Defined in: | actionview/lib/action_view/test_case.rb |
Instance Attribute Summary
Instance Method Summary
- #determine_default_helper_class(name)
- #helper_method(*methods)
- #new
-
#register_parser(format, callable = nil, &block)
Register a callable to parse rendered content for a given template format.
- #tests(helper_class)
Instance Attribute Details
#helper_class (rw)
[ GitHub ]# File 'actionview/lib/action_view/test_case.rb', line 183
def helper_class @helper_class ||= determine_default_helper_class(name) end
#helper_class=(value) (rw)
[ GitHub ]# File 'actionview/lib/action_view/test_case.rb', line 181
attr_writer :helper_class
Instance Method Details
#determine_default_helper_class(name)
[ GitHub ]#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)
Register a callable to parse rendered content for a given template format.
Each registered parser will also define a href="FORMAT">rendered.
helper method, where [FORMAT]
corresponds to the value of the format
argument.
By default, ::ActionView::TestCase
defines parsers for:
-
:html
- returns an instance ofNokogiri::XML::Node
-
:json
- returns an instance of::ActiveSupport::HashWithIndifferentAccess
These pre-registered parsers also define corresponding helpers:
-
:html
- definesrendered.html
-
:json
- definesrendered.json
Parameters
format
-
The name (as a
::Symbol
) of the format used to render the content. callable
-
The parser. A callable object that accepts the rendered string as its sole argument. Alternatively, the parser can be specified as a block.
Examples
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
To parse the rendered content into RSS, register a call to 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
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
# 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