Module: ActionView::TestCase::Behavior::ClassMethods

Defined in:
lib/action_view/test_case.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#helper_classObject



182
183
184
# File 'lib/action_view/test_case.rb', line 182

def helper_class
  @helper_class ||= determine_default_helper_class(name)
end

Instance Method Details

#determine_default_helper_class(name) ⇒ Object



162
163
164
165
166
# File 'lib/action_view/test_case.rb', line 162

def determine_default_helper_class(name)
  determine_constant_from_test_name(name) do |constant|
    Module === constant && !(Class === constant)
  end
end

#helper_method(*methods) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/action_view/test_case.rb', line 168

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

#inherited(descendant) ⇒ Object

:nodoc:



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/action_view/test_case.rb', line 66

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

#newObject



186
187
188
189
# File 'lib/action_view/test_case.rb', line 186

def new(*)
  include_helper_modules!
  super
end

#register_parser(format, callable = nil, &block) ⇒ Object

Register a callable to parse rendered content for a given template format.

Each registered parser will also define a #rendered.[FORMAT] helper method, where [FORMAT] corresponds to the value of the format argument.

Arguments

format - Symbol the name of the format used to render view’s content callable - Callable to parse the String. Accepts the String.

value as its only argument.

block - Block serves as the parser when the

<tt>callable</tt> is omitted.

By default, ActionView::TestCase defines a parser for:

* :html - returns an instance of Nokogiri::XML::Node
* :json - returns an instance of ActiveSupport::HashWithIndifferentAccess

Each pre-registered parser also defines a corresponding helper:

* :html - defines `rendered.html`
* :json - defines `rendered.json`

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


146
147
148
149
150
151
# File 'lib/action_view/test_case.rb', line 146

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) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/action_view/test_case.rb', line 153

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